By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

General question on catching exceptions

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/is...6/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
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
Thanks Jay, I just let my msdn subscription lapse, but I'll go buy that
issue.

Eric
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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/is...6/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
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.