Programming | Advanced 7.4.2 VBScript error handling
In VBScript, you can make the script interpreter not handle any errors it finds, and continue to run the next statement using the On Error Resume Next statement. Once this statement has been processed, the script engine will continue to run the following program without regard to any errors that have been discovered. However, this procedure applies only to the environment in which the statements are executed sequentially, in other words, not to nested functions or subroutines.
1. Use on Error Resume Next statement
When an error occurs in a subroutine, if the On Error Resume Next statement is not run, the error is given to the environment that invoked it, and the process repeats until it finds the environment that runs the On Error Resume Next statement, or finds the default script error handler. Give the error to ASP and IIS displays the default error page. This process is shown in Figure 7-16:
Figure 7-16 Error Handling process
This kind of error call chain means that you can create functions and subroutines that prevent run-time errors that cause the program to stop running. If you place an On Error Resume Next statement at the beginning of a subroutine, any run-time errors abort the subroutine, but the program that invokes the subroutine will continue to run without causing the Web page to stop.
For example, if you need to write a string to a file, you can access the file through a separate function to prevent the error from interrupting the entire program:
' Create a file named strFileName, overwriting any existing one and that name
' and writes strcontent into it then closes the file
' Returns True if it succeeds, or False on any error
Function writenewfile (strFileName, strcontent)
On error Resume Next ' turn ' default Error handler
Witenewfile = Flase ' default return value of function
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If err.number = 0 Then Set objfile = objFSO.CreateTextFile (strFileName, True)
If err.number = 0 Then objfile.writeline strcontent
If err.number = 0 Then objfile.close
If err.number = 0 Then writenewfile = True
End Function
Note that the above program checks the number property of the Err object in VBScript before attempting to process each program statement. If this value is 0 (no errors have occurred), then the process of setting up and creating the file can continue. However, if the error does occur, the script engine sets the value of the properties of the Err object and continues processing the next line.
The return value of the function is set to "True" as long as it does not cause an error to run correctly. Otherwise the function returns "False". In programming, you can use this function and take other actions after you have tested it.
Here's a simple example where we want to use a separate function for the first part of the task so that we can more precisely identify where the error is generated. This makes it easier to read the code when debugging. In the main program of the page, you can call three separate functions.
If createnewfile (strFileName) Then ' Create the new file
Response.Write "New file successfully created<br>"
If writecontent (strcontent) Then ' Write the content
Response.Write "Content written to File<br>"
Else
Response.Write "error:failed to Write to the file<br>"
End If
If CloseFile (strFileName) Then
Response.Write "File closed<br>"
Else
Response.Write "Error:failed to close the file<br>"
End If
Else
Response.Write "error:failed to create the new file<br>"
End Funciotn
2. Use on Error Goto 0
in ASP 2.0 (although not documented) and ASP 3.0, the On Error Goto 0 statement can also be used to recover the default fault handling behavior. When this statement is run, a run-time error occurs that will result in default error handling, checking each nested program in the environment chain until the home page code. If no other environment turns off default error handling, the execution of the Web page stops and displays the IIS default error page.
3. VBScript Err Object
In the previous example, when you turn off default error handling, check to see if the error has occurred by checking the number property of the VBScript Err object. The Err object stores information about Run-time errors, and Tables 7-3 and 7-4 show the methods and properties provided by the VBScript Err object.
Table 7-3 VBScript Err Object method
Method
Description
Clear
Clears all current Err object settings
Raise
Generate a Run-time error
Table 7-4 The properties of the VBScript Err object
Property
Description
Description
Sets or returns a string that describes the error
Number
(default) Sets or returns a value that specifies an error
Source
Sets or returns the name of the object that generated the error
Use these properties to check which error occurred. For example, you can take different measures depending on the error number, or you can use the source and Description property values to provide the user with an error message, or to send it to a file.
You can also use the Err object to generate an error. Why do you do this? Because sometimes you want to pass a custom error message to the user. You can set the properties of the Err object to whatever value you want. The Raise method is then invoked to produce this error, which stops the program from running and passes the error back to the call chain.
The following example shows how to handle errors when reading a text file on a server disk. Note how to use constant vbObjectError to determine that the error number you select is not confused with an existing error number. By adding arbitrary error numbers to this constant, you can guarantee that the predefined errors are not confused.
Functoin readthisfile (strFileName) ' returns ' content as a string
On Error Resume Next
Readthisfile = "" ' Default return value of function
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("strFileName", ForReading)
Select Case Err.Number
Case 0 ' OK, take no action
Case a ' standard file or path not found errors
' Create custom error values and raise error back to the call chain
Interrnumber = vbobjecterror + 1073 ' custom error number
Strerrdescrip
[1] [2] Next page