ASP On Error Resume Next

Last night I found a page with an “On Error Resume Next” that has a script error that has been causing problems on one of my servers for months. It took a long time to narrow down the source to a single site but then it didn’t take long to find the page with the problem. Then I found an “On Error Resume Next” line in the code and just wanted to shake my fist at somebody (in a good natured sort of way, of course). The code was really, really bad. But I was so relieved to have found the problem.

 

To clarify how this can be a problem, you don’t want code to continue to execute in a loop when there is an error. For example. This would be bad:

 

On Error Resume Next

Set rs = oConn.Execute(“Select * From Orders”)

If Not rs.Eof Then

                Do While Not rs.Eof

                                Response.Write(“OrderID=” & rs(“OrderID”) & “<br>”)

                                rs.MoveNxt

                Loop

End If

 

Since the error (“rs.MoveNxt”) is the move to the next record it will be stuck on the first record and the end of data will never be reached. Yes the script should timeout but an accumlation of errors can cause memory issues on the server and even hang the web service.

 

Here is the recommended way to use the ASP error handler:

 

On Error Resume Next

               

                … do code here

 

If Err.Number <> 0 Then

                sMessage=”There was an error: “ & Err.Description

                Err.Clear

                On Error Goto 0

End If