This is not how you should do it. You need to handle all exceptions in every outer event handling loop of the application. For this purpose,
System.Windows.Forms.Application
class has the following facility:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventhandler%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx[
^],
see also
http://msdn.microsoft.com/en-us/library/ms157905(v=vs.110).aspx[
^],
or
http://msdn.microsoft.com/en-us/library/ms223898(v=vs.110).aspx[
^].
First of all, in your entry-point method (
Main
), you need to set up this mode:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
And then, install an exception handler for the event
Application.OnThreadException
somewhere. I usually do it in the constructor of the main form, in the very beginning, just to use some form methods in the handler, but it could be done anywhere else, as soon as possible. The event arguments class
System.Threading.ThreadExceptionEventArgs
gives your the access to the exception instance (
ThreadExceptionEventArgs.Exception
). The sample code can be found here:
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx[
^].
—SA