Application Level Error Handling

  • Thread starter Jason MacKenzie
  • Start date
J

Jason MacKenzie

I have a production critical windows forms application. I have try catch
blocks everywhere to handle every eventuality. However occasionally, every
couple of months, the application crashes. Its usually a .Net Framework
error message telling me there was an unhandled exception

An example would be last October when the clocks rolled back. My question
is: is there a way to handle an exceptions that might make it through the
error handling at the application level?

Thanks,

Jason MacKenzie
 
J

Jason MacKenzie

Application level is the wrong way to word it. I think I'll put a try catch
block around entire methods and then for more granularity, keep the try
catch block within the "outer" try catches.

Is this a reasonable approach?
 
J

Jay B. Harlow [MVP - Outlook]

Jason,
Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
 
J

Jason MacKenzie

Thanks Jay. That was really helpful.


Jay B. Harlow said:
Jason,
Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
 
C

CJ Taylor

Regarding Windows forms.

You cannot catch a StackOverflowException nor a OutOfMemoryException'

bah!
=)
 
J

Jay B. Harlow [MVP - Outlook]

CJ,
I've had mixed luck with catching the StackOverflowException, I've been able
to catch it sometimes, and not other times... I don't remember the exact
cases where I was able to catch it, I want to say there was a thread & KB
article I participated in a while ago.

The only problem I see with catching OutOfMemoryException, is what are you
going to be able to do, there is no memory available to do anything ;-)

Just a thought
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top