Link to home
Start Free TrialLog in
Avatar of sybe
sybe

asked on

discussion: avoinding and using "On Error Resume Next"

offspin from https://www.experts-exchange.com/questions/20935064/problem-of-compare-the-value-of-the-date-system-and-the-date-we-pick-from-list-box.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)
ASKER CERTIFIED SOLUTION
Avatar of Slimshaneey
Slimshaneey
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mouatts
mouatts

>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
>>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.
Avatar of sybe

ASKER

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


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.