We help IT Professionals succeed at work.

discussion: avoinding and using "On Error Resume Next"

sybe
sybe asked
on
4,638 Views
Last Modified: 2012-03-15
offspin from https://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)
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
>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
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>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.

Author

Commented:
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
%>


CERTIFIED EXPERT
Top Expert 2012

Commented:
Those are all good exceptions to my rule of never use On Error Resume Next.
>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.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.