Try & Catch Blocks – Error handling in ASP

ASP Add comments

Error and exception handling in ASP is’n that easy. There are only two basic commands which help us to deal with situations where we don’t really know what may happen.
The default behavior of the script engine is to stop when an error occurs and to send the error description to the browser. The first command is to change this behavior to “whatever happens, go on”:

On Error Resume NextIf you wish to reactivate the default behavior:

On Error Goto 0These two commands and the “err” object help us to simulate a try-catch-usage. Let’s implement the VBscript way of try and catch:

‘Try
On Error Resume Next

‘Do something incorrect
Dim x
x = 3
x = x / 0

‘Catch
If err > 0 Then
    Response.Write “An error occurred: ” & Err.Description & ” (Error number: ” & Err & “)<br>”
    Err.Clear
End If
On Error Goto 0

Response.Write ” Result is: ” & x
As you can see here, it’s possible to deal with harmful situations. But be careful: Some programmers like it to write “On Error Resume Next” in the first line of their programs. That may be comfortable because no error will occur, but it’s not polite and makes bug fixing very hard.

 link to full article

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in