General question on catching exceptions

E

Eric Sabine

I built a generic exception handler form which allows the user to get
information from it, print it, email it, etc. for exceptions for which I
explicitly didn't handle in code, such as missing permissions on a stored
procedure, etc. I use this exception handler in all of my applications and
it can be called as simply as follows

try
...
catch ex as sqlexception
ExceptionHandler(ex, otherData1, otherData2)
catch ex as exception
ExceptionHandler(ex, otherData1, otherData2)
end try

As a habit, I tend to wrap every method and event within a try-catch block,
but now I am wondering if that is necessary. All of my apps start with Sub
Main which also contains a try-catch block that catches these 2 exceptions.
So if there isn't a specific exception within a method or event that I am
specifically testing for, or where I do something different that I've
already specified in Sub Main(), is it just a waste to catch an exception
there that I, by default, could already catch in Sub Main()?

Hope this makes sense.

Eric
 
J

Jay B. Harlow [MVP - Outlook]

Eric,
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/default.aspx

Unfortunately the article is not on-line yet.


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.

Is ExceptionHandler overridden? Do you really need the two calls, I would
have a single ExceptionHandler routine, something like:

Public Sub ExceptionHandler(ex As Exception, ...)
If TypeOf ex Is SqlException Then
' do Sql Exception specific stuff
Else
' Do other exception specific stuff
End If
End Sub

This way I only need a single catch clause, where I needed a try/catch.

Hope this helps
Jay
 
E

Eric Sabine

Thanks Jay, I just let my msdn subscription lapse, but I'll go buy that
issue.

Eric
 

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