473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Error handlers and Exception handlers

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
4 1812
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: VM | last post by:
Is it possible to intercept all exception errors into one Try...Catch? I've tried wrapping Application.Run() in a Try...Catch, but it doesn't seem to work all the time. Thanks.
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
20
by: DraguVaso | last post by:
Hi, As a former VB-programmer I'm used to the "on error goto"-errorhandling. I see that in actually all the VB.NET-samples I can fin people use the "Try - catch"-errorhandling. Which of the...
5
by: OHM | last post by:
Hi everyone, I have a problem with error handling and wondered if anyone has managed to implement a global exception handling model. Is it possible to ensure that you see all exceptions before...
5
by: Jason MacKenzie | last post by:
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...
2
by: Lucas Tam | last post by:
Without writing try/catch statements is there a way to have a global error handler in VB.NET? In ASP.NET there is an application level error handler inside Global.asax, but is there something...
4
by: Paul Wilson | last post by:
I want to use Err.Raise() method to raise my own exceptions. Is this the right way of raising my own exceptions ? (i think this is the only way). What is the Error number i can safely use,...
8
by: jcrouse | last post by:
I am using the following code to trap errors in a sub routine: Try Executable code Catch ex As Exception Dim strInputE As String = Application.StartupPath & "\Error.txt" Dim srE As...
7
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.