Help - Search - Members - Calendar
Full Version: [C#] Catch Application exception
Neowin Forums > Help & Discussion Center > Programming (C#, C++, JAVA, VB, .NET etc.)
zackiv31
CODE
            try
            {
                Application.Run(new Form1());
            }
            catch (Exception e)
            {
                 //report exception to server


Through visual studio, the above catch block is actually invoked. (I'm using it for error reporting back to my site, to fix problems and such).


But when I run the standalone exe, the exception is thrown into .NET's exception handling mechanism, and thus is never caught by my try catch block. It mentions JIT (just in time debugging), but I don't know if it has to do with it.


Is this the right way I should be doing this? It makes sense and works through Visual Studio, but not as standalone (which I need).
_kane81
maybe

catch (RuntimeException e)
zackiv31
I don't think it exists in C#... anyone else? I thought Exception was the most general of errors and could be caught..
S7un7
Did you read what type of exception .Net is saying has occured?
zackiv31
I'm throwing the exception on my own...

The point is that in visual studio it propogates up the calling stack to the first Application.Run... but when run as a stand alone, .NET steps in for the error catching... which I don't want it to do. I want my catch at the top to handle it all..

That make sense?
Winston
What you're after is global exception handling, and there's an event you need to subscribe to in order to catch this and not let the .NET framework spit out the dialog, I forgot the event name but it's under Application object, something like Application.ThreadException

Look for an event under that object, and subscribe to it.
WebWacker
Quote - (Winston @ Feb 12 2008, 12:55) *
What you're after is global exception handling, and there's an event you need to subscribe to in order to catch this and not let the .NET framework spit out the dialog, I forgot the event name but it's under Application object, something like Application.ThreadException

Look for an event under that object, and subscribe to it.


You can read this for more information. See below for a quote which may point you in the right direction.

Quote -
By default, unhandled exceptions in a Windows Forms application will display the Unhandled Exception Dialog. You can get the behavior you want by either hooking the Application.ThreadException event (rather than the AppDomain.UnhandledException event) or by setting Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException) in your Main() method.
zackiv31
Winston and WebWacker thanks! A quick read of that article and calling one method solved all my problems:

CODE
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.