On Error resume next: VBScript error handling

Source: Internet
Author: User

In VBScript, the script interpreter does not handle any errors it finds, and runs the next statement using the on error resume next statement. Once the statement has been processed, the script engine will continue to run the subsequent program, regardless of any errors that have been found. However, this process is only applicable to environments where statements are executed sequentially. In other words, it is not applicable to nested functions or subprograms.
1. Use the on error resume next statement
When an error occurs in a subroutine, if the on error resume next statement is not run, the error will be handed over to the environment that calls it, this process repeats until you find the environment where the on error resume next statement is run, or find the default Script Error processor, submit the error to ASP, and IIS displays the default error webpage.
This error call chain means that you can create functions and subprograms that prevent running errors. If you place an on error resume next statement at the beginning of a subroutine, the running of this subroutine will be suspended for any runtime error, but the program that calls this subroutine will continue to run without causing the web page to stop.
For example, if you want to write strings to a file, you can use an independent function to access the file to prevent errors from interrupting the entire program:
''''' Create a file named strfilename, overwriting any existing one with 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 off the 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 preceding program checks the number attribute of the ERR Object of VBScript before trying to process each program statement. If the value is 0 (no error has been reported), you can continue the File Import and creation process. However, if an error occurs, the script engine sets the attribute value of the ERR Object and continues to process the next row.
If the function runs normally without causing an error, the return value of the function is set to "true ". Otherwise, the function returns "false ". In programming, you can use this function and take other actions after testing it.
The following is a simple example. We hope to use an independent function for the first part of the task to more accurately identify where errors are generated. In this way, the code is easier to read during debugging. In the main program of the page, you can call three independent 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
The on error goto 0 statement can be used to restore the default error handling behavior in ASP 2.0 (although no document is recorded) and ASP 3.0. After running this statement, the runtime errors will cause default error handling. Check each nested program in the Environment chain until the homepage code is displayed. If no other environment is available to disable default error handling, the execution of the web page stops and the IIS default error web page is displayed.
3. VBScript ERR Object
In the previous example, when the default error handling is disabled, check the number attribute of the VBScript ERR Object to check whether the error has occurred. Err objects store information about runtime errors. Table 7-3 and Table 7-4 provide methods and Attributes provided by VBScript err objects.
Clear
Clear all current ERR Object settings

Raise
Generate a runtime error

VBScript ERR Object Attributes

Description
Sets or returns a string with an incorrect description.

Number
(Default) set or return a specified error value

Source
Set or return the name of the object that produces the error

You can use these attributes to check which errors have occurred. For example, you can take different measures based on the error number, or use the attribute values of source and description to provide an error message for the user, or transfer it to a file.
You can also use the ERR Object to generate an error. Why? Because sometimes you want to send a custom error message to the user. You can set the attribute of the ERR Object to any expected value. Call the raise method to generate this error. In this way, the program stops running and the error is passed back along the call chain.
The following example shows how to handle errors when reading a text file on a server disk. Note how to use the constant vbobjecterror to ensure that the selected error number is not confused with an existing error number. By adding any selected error number to this constant, we can avoid confusion with predefined errors.
Functoin readthisfile (strfilename) ''' returns the 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 50, 53 ''' standard file or path not found errors
''''' Create M error values and raise Error Back up the call chain
Interrnumber = vbobjecterror + 1073 ''' M error number
Strerrdescription = "the file has been deleted or moved ."
Strerrsource = "readthisfile function"
Err. Raise interrnumber, strerrsource, strerrdescription
Exit Function
Case else ''' som other error
''' Raise the standard error back up the call chain
Err. Raise err. Number, Err. Source, Err. Description
Exit Function
End select
Readthisfile = objfile. readall ''''' we opened it OK, so return the content
Objfile. Close
End Function
The code that calls this function can use the on error resume next statement and capture the errors produced by this function.
On Error resume next
Strcontent = readthisfile ("myfile, TXT ")
If err. Number = 0 then
Response. Write "file content is: <br>" & strcontent
Else
Response. Write "Err. Source &" <br> "& err. Description
End if

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.