Solved

discussion: avoinding and using "On Error Resume Next"

Posted on 2004-03-29
12
4,495 Views
Last Modified: 2012-03-15
offspin from http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20935064.html

In my opinion one shoud not use "On Error Resume Next" for a complete ASP page. In some (rare) situations it has a use within funcions, like with XML.

Most errors can be trapped using the native IsDate(), IsNumeric(), IsObject(), TypeName(), FSO.FileExists etc functions.  

Some functions that are not native in VBScript can be created, like
- IsEmail(), to check is a string is a valid email address
- IsXML(), the MSXML object has it's own error trapping

I think it is an important issue because I see many (starting?) programmers using "On Error resume Next" at the top of their code. And I have the habit of remarking that "don't do that".

Any opinions?

(points will be distributed among meaningfull/contributing comments)
0
Comment
Question by:sybe
  • 3
  • 2
  • 2
  • +2
12 Comments
 
LVL 11

Accepted Solution

by:
Slimshaneey earned 125 total points
Comment Utility
I think you are right. The reason On Error Resume Next was added to ASP/VB was to save coders time in writing all the extra error checking code. There are a lot of lazy coders and people who are serious about ASP or VB I doubt would ever likely use On Error Resume Next.
0
 
LVL 12

Assisted Solution

by:monosodiumg
monosodiumg earned 125 total points
Comment Utility
The practice of putting an error handler at the top level: I agree. I'm not at all keen on "resume Next" though. Any "resume next" error handler can be rewritten using a proper label and with an "on error" that  is not at the top level but immediately
before the point where the error is anticipated. One problem with "resume Next" is that you don't know if anything ever happened. IMO it can't make sense to continue running code regardless of what error(s) have occured. My error handlers resume at labelled points if the error raised is one than can be handled and otherwise they do not resume and let the error propagate up.

There is a grey area where strictly speaking an error can be checked for instead of using error handling but it's very cumbersome to do so. Suppose you read from a file within a loop. Are you really going to do a FSO.FileExists on every iteration? I do it once before the loop but I don't check again on every iteration even though in theory the file could vaninsh or become unavailable between calls. This still doens't justify "resume next".


Slimshaneey:
>The reason On Error Resume Next was added to ASP/VB was to save coders time in writing all the extra error checking code.
I wans't involved in the desing of VB but I doubt very much that your statment is correct. Error (aka exception) handling isn't there to support lazy progrmming. It's there so that you can trap errors. You are free to misues it to trap errors that you could pick up through proper checking, like division by zero, but there are errors that can occur that the programmer can not check for such as hardware failures, external applications misbehaving, write failures, resource shortages,....) .  Error handling should be reserved for these.
Every language used for real-world applications must have an error handling mechanism.
0
 
LVL 11

Assisted Solution

by:mouatts
mouatts earned 125 total points
Comment Utility
One of the things I have impressed on the many trainees that I have had and often through the pages of EE is that do things in such a way that isn't going to cause problems for someone who comes to your code later (or yourself when you return to code you haven't looked at for years!).

The On error can fall into the category of things not to use, or over use. All too often if you hide an error, which is what on error resume next really does, it will come back to haunt you further on. I've lost count of the number of times that a novice consentrates on the syntax of a statement that has fallen over whereas the problem actually occured earlier but has been surpressed. The experienced amongst us are more likely to look at the iinputs the offending line and thus see the actual fault than a novice, but if we are honest even the most experienced will have been caught out by this sort of thing more than once.

The longer that an untrapped error is allowed to persist not only is it harder to find but the more damage it can cause.

I don't think there is one single approach to error handling. Certainly I have come across examples where if an error occurs I want it to blow up in the users face simply because to attempt to handle or recover from the error situation is likely to cause more problems and damage, and the situation is serious enough that we want the alarms to go off.  Within the context of ASP such a situation calls for no error handling at all.
Equally very occasionally I have come across the situation where an error can be ignored and this is prehaps one of the few occasions where On Error is valid to use.

monosodiumg comment 'Are you really going to do a FSO.FileExists on every iteration' reminded my of an occasion when a Systems Programmer said to me "you can't check every error condition" this was just after his code had trashed our database and we looked forwards to 4 weeks work loading it up again!

On any ASP project I have a line at the top that includes a library of routines as a matter of course. Thats fine because there is a reason for it but but putting an On Error Resume Next in as a matter of course is not unless we have a definiate reason for it.

Steve
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 125 total points
Comment Utility
For the record,  On Error ... syntax dates long before VB (yes, there was life before VB !) and to the early BASIC years.  VBScript only supports On Error Resume Next (unlike VB which supports a more "robust" On Error GoTo label)  On Error Resume Next is the only way you can detect errors in VBScript.  Having said that, I have very rarely had the need to use it and would go even further than sybe and say that it is not even needed in XML, at least I have never had a need for it.  The only place for it IMHO is when connecting to a database and even then it should be wrapped very tightly around the code, as in:
On Error Resume Next
cn.Open
On Error GoTo 0

If any of you remember a few years back (OK about 30 !) the famous computing phrase was "GoTo's are evil".  I would add that On Error Resume Next is also evil.

Thankfully with .NET all of this is ancient history and we now have better tools to handle exceptions.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:mouatts
Comment Utility
>On Error Resume Next is the only way you can detect errors in VBScript
in ASP 2 maybe but we have the error object that allows us to test for errors - anyhow I'd argue that On Error doesn't allow us to detect errors it allows us to sort of supress them.

Steve
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>we have the error object that allows us to test for errors<
Unfortunately, unless you use On Error Resume Next the Error object will not be much use to you.
0
 
LVL 28

Author Comment

by:sybe
Comment Utility
I have a few examples where I use On Error Resume Next in a function. Because I don't know how to do what i want in an other way.

<%
' is there another way to get the dimension of an array???

Function GetArrayDimension(ByVal aArray, ByVal iMax)
    Dim iCount, iTest
    On Error Resume Next
    For iCount = 1 To iMax
        iTest = UBound(aArray,iCount)
        If Err Then Exit For
    Next
    GetArrayDimension = iCount -1
    Err.Clear
End Function
%>


<%
' create a connection. There used to be some strange bug in IIS that would with irregular interval require
' "DSN=" before the actual connecion string. The code below is ages old, and i haven't checked it's necessity for a long time

Function MakeDSNConnection(sDSN)
    Dim oConn

    Set oConn = Server.Createobject("ADODB.Connection")
    On Error Resume Next
    oConn.Open sDSN
    If Err Then
        Response.write Err.description & "<br>"
        sDSN = "DSN=" & sDSN
        oConn.Open sDSN
    End If
    Set MakeDSNConnection = oConn
End Function
%>


<%
' find out if an object is a Collection

Function IsCollection(ByVal oVar)
    On Error Resume Next
    Dim element, vTest
    IsCollection = False
    For each element In oVar
        If Err Then
            Err.Clear
            Exit Function
        Else
            vTest = oVar(element)
            If Err Then
                Err.Clear
                Exit Function
            Else
                IsCollection = True
                Exit Function
            End If
        End If
    Next
    Err.Clear
End Function
%>


0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Those are all good exceptions to my rule of never use On Error Resume Next.
0
 
LVL 12

Expert Comment

by:monosodiumg
Comment Utility
>is there another way to get the dimension of an array???
Yes: http://vbnet.mvps.org/index.html?code/helpers/getarraydims.htm

>find out if an object is a Collection
Can't think of another way of doing that but I've never come across a situation where such basic type info is now already known.

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now