Pages

Thursday, August 18, 2011

Use On Error Resume Next sparingly (VBScript 4.0+)

Normally, when VBScript code produces an error, it throws an exception and the ASP page immediately terminates. Unfortunately, there may be times, especially while debugging, when you need the page to continue despite the error. In such cases, instead of writing a special error handler, use the statement: On Error Resume Next instead. This statement allows VBScript to return to the point in the code after where the error occurred and continue normal processing.

However, you should be careful to use this statement only during the debug process or in cases where you know a particular error might occur, but there’s no harm in ignoring it. Consider the following code:

Function PrintFile
On Error Resume Next
‘....Printing code that may fail
PrintFile = Err.Number
End Sub

As you can see, this is an instance where the statement is programmatically necessary, since you don’t want the page to terminate just because it couldn’t print. If you’re debugging and you simply want to ignore an error that’s occurring in a specific piece of code, place On Error Resume Next at the start of the offending code and On Error GoTo 0 at the end, like so:

On Error Resume Next
‘ ....Some code that may fail
On Error GoTo 0
‘....More code.

In this case, the VBScript will ignore any errors that occur between the two sets of statements, but after the statements, it will trigger exceptions as usual. Except in cases where it’s programmatically necessary, you should remove all On Error Resume Next statements from your application prior to installation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.