VbScript On Error Resume Next fault-tolerant use experience _vbs

Source: Internet
Author: User
Tags error handling function definition goto
In VBScript, error handling is done using On Error Resume Next, and if you add this sentence to your code, the other code after this sentence will ignore the error and continue to run the following code, and we can use the code below to catch the error

Copy Code code as follows:

If Err.number<>0 Then
Errnum = Err.Number
Errdesc = Err.Description
Call G00b02logout ("error.002", Errnum, Errdesc)
End If
If Err.number<>0 Then
Errnum = Err.Number
Errdesc = Err.Description
Call G00b02logout ("error.002", Errnum, Errdesc)
End If

This captures the error code and the description of the error and writes to the log file. However, the problem is that after we capture the code after this error, the code will still ignore the error and continue to run if there is an error and you are not capturing it. Ignoring errors is a result that we do not want to see, and will result in difficulty in debugging programs. At this point you can use on Error GoTo 0 to terminate the previous error handling, that is, you can and on Error Resume next pair appear. This will not affect the following code.

Copy Code code as follows:

On Error Resume Next
。。。
' DB operate.
。。。
If Err.number<>0 Then
Errnum = Err.Number
Errdesc = Err.Description
Call G00b02logout ("error.002", Errnum, Errdesc)
End If
On Error Goto 0
On Error Resume Next
。。。
' DB operate.
。。。
If Err.number<>0 Then
Errnum = Err.Number
Errdesc = Err.Description
Call G00b02logout ("error.002", Errnum, Errdesc)
End If
On Error Goto 0

There are several features to be understood,
1,on Error Resume Next if in global definition, the function is global, you use this sentence in the main program, if a function is called, then if there is an error in the function, it will be ignored, you can also catch this error after the statement of function call in the main program. This can be verified by the following simple code:

Copy Code code as follows:

On Error Resume Next
FUNCB ' Call function
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
WScript.Echo "Main ..."
' On Error Goto 0
Sub FUNCB
AAAAAAAAAAA ' Invalid statement for test
WScript.Echo "FUNCB OK"
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
End
On Error Resume Next
FUNCB ' Call function
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
WScript.Echo "Main ..."
' On Error Goto 0
Sub FUNCB
AAAAAAAAAAA ' Invalid statement for test
WScript.Echo "FUNCB OK"
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
End

Execute the results of the above code:
13-type が consistent しません.
Main .....
As you can see, in the function, AAAAAAAAAAA is deliberately made an error, followed by the WScript.Echo "FUNCB OK" and subsequent code are not executed. However, the WScript.Echo "main ..." statement in the main program is executed. That is, if a statement fails in a function, subsequent statements in the function do not execute directly, executing the statement following the statement that called the function.
2,on Error Resume Next if defined within a function, look at the following code execution

Copy Code code as follows:

FUNCB ' function call
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
WScript.Echo "Main ..."
Sub FUNCB ' function definition
On Error Resume Next
AAAAAAAAAAAA ' Invalid statement
WScript.Echo "FUNCB OK"
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
End Sub
FUNCB ' function call
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
WScript.Echo "Main ..."
Sub FUNCB ' function definition
On Error Resume Next
AAAAAAAAAAAA ' Invalid statement
WScript.Echo "FUNCB OK"
If Err. Number <> 0 Then
Errnum = Err.Number
Errdesc = Err.Description
WScript.Echo Errnum & "-" & Errdesc
End If
End Sub

The results of the implementation are as follows:
FUNCB OK
13-type が consistent しません.
13-type が consistent しません.
Main .....
As you can see, this error can be caught in the Err object in the function body and in the main program that invoked it, which indicates that the Err object is global and should be understood so that the scope of err works between an On Error Resume Next statement and an on error Goto 0. If we add an invalid statement after the FUNCB call statement, execution will pop up the error msg box, stating that the On Error Resume next in the function body does not function outside of the function body.
The above is the use of on the error Resume next experience, if you understand the above two points, you can better use the function of error handling.

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.