VBScript on error resume next Fault Tolerance experience

Source: Internet
Author: User

CopyCode The Code is 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

In this way, the error code and description information can be captured and written into the log file. However, the problem is that the Code after the error is captured will still ignore the error and continue to run if the error occurs again but is not captured. The ignore error is the result we do not want to see.ProgramDebugging is difficult. In this case, you can use the on error goto 0 clause to terminate the preceding error handling. This can be paired with on error resume next. This will not affect the subsequent code.

Copy code The Code is 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 understand,
1. If on error resume next is defined globally, the function is global. You have used this sentence in the main program. If a function is called later, if an error occurs in the function, it will be ignored. You can also catch this error after the function call statement in the main program. This can be verified by the following simple code:

Copy code The Code is as follows: on error resume next
Funcb 'Call a 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 used for testing
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 a 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 used for testing
Wscript. Echo "funcb OK"
If err. Number <> 0 then
Errnum = err. Number
Errdesc = err. Description
Wscript. Echo errnum & "-" & errdesc
End if
End

Result of executing the above Code:
13-all types of containers must be consistent.
Main ....
It can be seen that in the function, aaaaaaaaaaa is a deliberate error, and the subsequent wscript. Echo "funcb OK" and subsequent Code are not executed. However, the wscript. Echo "Main..." statement in the main program is executed. That is to say, if a statement in a function fails, all subsequent statements in the function are not executed, and the statements following the statements that call the function are directly executed.
2. If on error resume next is defined in the function, check the code execution below.

Copy code The Code is 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 execution result is as follows:
funcb OK
the 13-type sequence is consistent with the sequence operator.
the 13-type sequence is consistent with the sequence.
main ....
as you can see, this error can be captured in both the ERR Object in the function and the main program that calls it. This indicates that the ERR Object is global and should be understood as follows, the scope of err is effective between one on error resume next statement and one on error goto 0. If an invalid statement is added after the funcb CALL statement, an error MSG box will pop up during execution, indicating that on error resume next in the function cannot be used in vitro.
the above are some experiences using on error resume next. If you understand the above two points, you can better use the error handling function.

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.