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

Error handlers and Exception handlers

P: n/a
sm
Hi,

I have a couple of questions with regards to handling errors and exceptions.

1. If I use

On Error goto Errhandler
...
Errhandler:

in all the routines will exceptions be also trapped by the 'On Error'?

2. Is using exceptions handling an alternative to using 'On Error' handlers?

Thanks in advance.
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"sm" <sm@discussions.microsoft.com> schrieb:
I have a couple of questions with regards to handling errors
and exceptions.

1. If I use

On Error goto Errhandler
..
Errhandler:

in all the routines will exceptions be also trapped by the 'On Error'?
Exceptions raised in methods called after 'On Error GoTo...' will be handled
too.
2. Is using exceptions handling an alternative to using 'On Error'

handlers?

Yes.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2

P: n/a
sm,
In addition to Herfried's comments.

There have been many a thread of the benefits of Try/Catch verses On Error,
if you would like links on these discussions I can find them & post them. I
prefer Try/Catch & Try/Finally

I use Try/Catch when there is something specific that I need to do with the
exception. Otherwise I let my global exception handlers handle the event.
IMHO logging should go in the global exception handler. Too often I've seen
Try/Catch blocks where the exception information is "lost" making it harder
to diagnose problems.

I use Try/Finally blocks significantly more often to ensure that resources
are closed, using the "using" statement in C# simplifies this. We will have
to wait for Whidbey (VS.NET 2005) to get the "Using" statement in VB.NET.
For an unhandled exception:
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...T/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
"sm" <sm@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
Hi,

I have a couple of questions with regards to handling errors and
exceptions.

1. If I use

On Error goto Errhandler
..
Errhandler:

in all the routines will exceptions be also trapped by the 'On Error'?

2. Is using exceptions handling an alternative to using 'On Error'
handlers?

Thanks in advance.

Nov 21 '05 #3

P: n/a
sm

Thanks a lot. Your reply has been very helpful. So is the link.

BTW, what is IMHO logging? I hope its not a naive question.

Regards.

"Jay B. Harlow [MVP - Outlook]" wrote:
sm,
In addition to Herfried's comments.

There have been many a thread of the benefits of Try/Catch verses On Error,
if you would like links on these discussions I can find them & post them. I
prefer Try/Catch & Try/Finally

I use Try/Catch when there is something specific that I need to do with the
exception. Otherwise I let my global exception handlers handle the event.
IMHO logging should go in the global exception handler. Too often I've seen
Try/Catch blocks where the exception information is "lost" making it harder
to diagnose problems.

I use Try/Finally blocks significantly more often to ensure that resources
are closed, using the "using" statement in C# simplifies this. We will have
to wait for Whidbey (VS.NET 2005) to get the "Using" statement in VB.NET.
For an unhandled exception:
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...T/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
"sm" <sm@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
Hi,

I have a couple of questions with regards to handling errors and
exceptions.

1. If I use

On Error goto Errhandler
..
Errhandler:

in all the routines will exceptions be also trapped by the 'On Error'?

2. Is using exceptions handling an alternative to using 'On Error'
handlers?

Thanks in advance.


Nov 21 '05 #4

P: n/a
sm,
IMHO = In my humbly opinion.

Most of the time I have exception logging in my global exception handler,
however I also have routines where I need the exception logging in a
specific routine. Such as processing items in a batch of, individual items
in the batch may fail, however the entire batch does not fail.

Hope this helps
Jay

"sm" <sm@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...

Thanks a lot. Your reply has been very helpful. So is the link.

BTW, what is IMHO logging? I hope its not a naive question.

Regards.

"Jay B. Harlow [MVP - Outlook]" wrote:
sm,
In addition to Herfried's comments.

There have been many a thread of the benefits of Try/Catch verses On
Error,
if you would like links on these discussions I can find them & post them.
I
prefer Try/Catch & Try/Finally

I use Try/Catch when there is something specific that I need to do with
the
exception. Otherwise I let my global exception handlers handle the event.
IMHO logging should go in the global exception handler. Too often I've
seen
Try/Catch blocks where the exception information is "lost" making it
harder
to diagnose problems.

I use Try/Finally blocks significantly more often to ensure that
resources
are closed, using the "using" statement in C# simplifies this. We will
have
to wait for Whidbey (VS.NET 2005) to get the "Using" statement in VB.NET.
For an unhandled exception:
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...T/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
"sm" <sm@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
> Hi,
>
> I have a couple of questions with regards to handling errors and
> exceptions.
>
> 1. If I use
>
> On Error goto Errhandler
> ..
> Errhandler:
>
> in all the routines will exceptions be also trapped by the 'On Error'?
>
> 2. Is using exceptions handling an alternative to using 'On Error'
> handlers?
>
> Thanks in advance.


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.