Exception Handling

  • Thread starter Thread starter Bala Nagarajan
  • Start date Start date
B

Bala Nagarajan

Hello All,
Is there a way to implement an excpetion handling for an
application domain? I am developing a windows application and i want to
catch any unhandled exceptions. I want to implement something similar to
Application_Error event in ASP.NET that is used to catch all the unhndled
exception.

Thanks

-Bala
 
Bala Nagarajan said:
Is there a way to implement an excpetion handling for an
application domain? I am developing a windows application and i want to
catch any unhandled exceptions. I want to implement something similar to
Application_Error event in ASP.NET that is used to catch all the unhndled
exception.

Take a look at the 'AppDomain.UnhandledException' event.
 
Hi Bala,

Thanks for your post.

Based on my understanding, you want to apply a global exception handler to
handle all unhandled exceptions in the appdomain, yes?

Yes, .Net provided several global unhandled exception handlers. For
Winform, we can use Application.ThreadException to handle unhandled
exception in main GUI thread. However, .Net defaultly will swallow
exceptions come from non-Main thread. To handle this worker thread
exception, we can add a AppDomain.UnhandledException event handler for
current appdomain.

The good article below shows us a general pattern for handling unexpected
error in .Net application, for your information:
"Unexpected Errors in Managed Applications"
http://msdn.microsoft.com/msdnmag/issues/04/06/NET/

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Bala,

Does our replies make sene to you? If you still have any concern, please
feel free to tell us, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Put this code into your main entry point of your app.

Application.ThreadException +=new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Then add this exception handler method.

private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

{

MessageBox.Show(e.Exception.Message + "\r\n" + e.Exception.StackTrace);

MessageBox.Show("Global exception");

}

Of course the event handler can be customized to fit your needs

Rgds

John
 
Back
Top