In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to handle errors in vbScript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
In vbscript, error handling is done using on error resume next. If you add this sentence to your code, if there are errors in other code after this sentence, the system will ignore these errors and continue to run the following code. At the same time, we can use the following code to catch errors.
The copy code is as follows:
If err.number0 then
ErrNum = Err.Number
ErrDesc = Err.Description
Call G00B02logOut ("Error.002", errNum, errDesc)
End If
If err.number0 then
ErrNum = Err.Number
ErrDesc = Err.Description
Call G00B02logOut ("Error.002", errNum, errDesc)
End If
In this way, the error code and description of the error can be captured and written to the log file. However, there is a problem with the code after we catch this error, and if there is another error and you don't catch it, the code will still ignore the error and continue to run. Ignoring errors is a result we do not want to see and will make it difficult to debug the program. At this point, you can use the sentence On Error GoTo 0 to terminate the previous error handling, that is, it can be paired with on error resume next. In this way, the later code will not be affected.
The copy code is as follows:
On error resume Next
. . .
'DB operate.
. . .
If err.number0 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.number0 then
ErrNum = Err.Number
ErrDesc = Err.Description
Call G00B02logOut ("Error.002", errNum, errDesc)
End If
On Error Goto 0
There are several characteristics to understand.
1 if error resume Next is defined globally, the effect is global. If you use this sentence in the main program, if you call a function later, if there is an error in the function, it will also be ignored. You can also catch this error after the statement of the function call in the main program, which can be verified by the following simple code:
The copy code is 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
Invalid statement for aaaaaaaaaaa '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
Invalid statement for aaaaaaaaaaa 'test
WScript.Echo "funcb ok"
If err.Number 0 Then
ErrNum = Err.Number
ErrDesc = Err.Description
WScript.Echo errNum & "-" & errDesc
End If
End
The result of executing the above code:
Type 13 is consistent with each other.
Main....
As you can see, in the function, aaaaaaaaaaa is deliberately making an error, and the subsequent WScript.Echo "funcb ok" and the following code are not executed. However, the WScript.Echo "main in the main program." Statement is executed. That is, if a statement goes wrong in a function, the following statements in the function are not executed, and the statements following the statement that called the function are executed directly.
2 error resume Next on the function if defined in the function, see the following code execution
The copy 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 implementation results are as follows:
Funcb ok
Type 13 is consistent with each other.
Type 13 is consistent with each other.
Main....
As you can see, this error can be caught both in the err object in the function body and in the main program that calls it, indicating that the err object is global. It should be understood that the scope of err is valid between an On error resume Next statement and an On Error Goto 0. If we add an invalid statement after the funcb call statement, an error msg box will pop up during execution, indicating that the On error resume Next in the function body cannot act on the outside of the function.
This is the end of the article on "how to handle errors in vbScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.