• 0

[C#] Catch Application exception


Question

			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).

Link to comment
https://www.neowin.net/forum/topic/619252-c-catch-application-exception/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

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?

  • 0

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.

  • 0
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.

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.
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.