ASP's ability to trap for errors is pretty weak compared to other languages. However, you can trap for errors, suppress errors, and stop suppressing errors. When an error is found, you can also handle it.
Suppressing Errors
To start trapping errors, add the following line:
On Error Resume Next
This will suppress all errors for the duration of the script. For example, normally the following code would cause the death of your code with a division by error message:
Dim x x = 1/0
However, you can suppress the error as follows:
On Error Resume Next Dim x x = 1/0
The above code will cause NO error at all.
Capture Error But Keep Going
While trapping for errors, you can handle the error by inspecting the Err object. The following code snippet handles our division by zero error by displaying a message but the script keeps going:
On Error Resume Next Dim x x = 1/0
If Err.Number <> 0 Then Response.Write "Error: " & Err.Description End If
Response.Write "Note, this line here still executes and prints this text."
Stop Suppressing Errors
After calling On Error Resume Next, you can stop suppressing errors with the following code snippet:
On Error Goto 0
Scope of the Err Object
The scope of the Err object exists ONLY until you call On Error Goto 0. For example, the following code snippet does NOT display the Err.Description text:
On Error Resume Next Dim x x = 1/0 On Error Goto 0
''' Err Object no longer exists! '''
If Err.Number <> 0 Then '''The following line does NOT print. ''' Response.Write "Error: " & Err.Description End If
Using On Error for Debugging
You can use On Error for debugging. The following example illustrates. The following line of code will fail if passed a null value. It may work in testing if your CurrentActualDate field always has a value.
Here is how to use On Error to selectively display the value of this variable but only when FormatDateTime fails.
On Error Resume Next Response.Write FormatDateTime(f_CurrentActualDate, vbShortDate)
If ErrNumber <> 0 Then Break(f_CurrentActualDate) End If
On Error Goto 0
Err Object Scope Is NOT Limited to a Method
Despite what I've read recently on some message boards, you can call On Error Resume Next from within a function or subroutine. For example, the following code snippet DOES display our division by zero error:
MyErrorSub If Err.Number <> 0 Then '''HandleError Err.Description Response.Write "Error: " & Err.Description End If
Sub MyErrorSub On Error Resume Next Dim x x = 1/0 End Sub
Make CDO.Send More Robust
On a website hosting server I was using recently, the CDO.Send method was failing sometimes because of a capacity issue the vendor was having. I needed a way to trap for failure and try again. You can use On Error Resume Next to trap for and retry sending of an email using CDO.
On Error Resume Next objCDOMail.Send
If err.number = 0 Then SU_SetActionMessage("Email sent....") Else SU_SetActionMessage("Error: Email did NOT send. Try again.") End If On Error GoTo 0
Here is the complete example I settled on:
On Error Resume Next Dim Counter
Counter = 1 While Counter < 10 objCDOMail.Send
If Err.Number = 0 Then If Counter = 1 Then Response.Write("Email sent...") Else Response.Write("Email sent... (Attempts needed: " & Counter & ")") End If
Counter = 10 Else Counter = Counter + 1
If Counter => 10 Then Resonse.Write("Email NOT sent (10 attempts failed). If important, try again.") End If End If WEnd On Error GoTo 0