In vbscript, error handling is done using on error resume next. If this sentence is added to your code, if there is an error in other codes after this sentence, the system will ignore these errors and continue to run the subsequent code. At the same time, we can use the following code to catch the error
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
This allows you to capture the error code and error description information and write it to the log file. However, there is a problem that after we catch this error, if the error occurs again and you do not catch it, the code will still ignore the error and continue to run. Ignoring errors is a result we do not want to see, which will lead to difficulty in debugging the program. At this time, you can use the On Error GoTo 0 sentence to terminate the previous error processing, that is, you can appear in pairs with on error resume next. This will not affect the subsequent code.
on error resume Next
。。。
'DB operate。。
。。。
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
On Error Goto 0
on error resume Next
。。。
'DB operate。。
。。。
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
On Error Goto 0
There are several characteristics that need to be understood.
1. On error resume Next, if it is defined globally, the function is global. You use this sentence in the main program. If a function is called later, if there is an error in the function, it will be ignored. You can also catch this error after the statement called by the function in the main program. This can be verified by the following simple code:
On error resume Next
funcb 'Call function
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
'On Error Goto 0
Sub funcb
aaaaaaaaaa'Invalid statement for testing
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End
On error resume Next
funcb 'Call function
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
'On Error Goto 0
Sub funcb
aaaaaaaaaa'Invalid statement for testing
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End
The result of executing the above code:
13 - The same type.
main ....
It can be seen that in the function, aaaaaaaaaaa is an error created on purpose, and the subsequent "funcb ok" and the subsequent code are not executed. However, the "main..." statement in the main program is executed. That is to say, if a statement in a function error occurs, the statements behind the function will not be executed, and the statements behind the statements that call the function are directly executed.
2. On error resume Next If defined in the function, see the execution of the following code
funcb 'function call
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
Sub funcb 'function definition
On error resume Next
aaaaaaaaaaaa'Invalid statement
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End Sub
funcb 'function call
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
Sub funcb 'function definition
On error resume Next
aaaaaaaaaaaa'Invalid statement
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End Sub
The execution results are as follows:
funcb ok
13 - The same type.
13 - The same type.
main ....
It can be seen that this error can be caught in the err object in the function body and the main program that calls it. This shows that the err object is global. It should be understood in this way 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 outside the function body.
The above are some experiences when using On error resume Next. If you understand the above two points, you can better use the error handling function.
Copy the codeThe code is as follows:
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
This allows you to capture the error code and error description information and write it to the log file. However, there is a problem that after we catch this error, if the error occurs again and you do not catch it, the code will still ignore the error and continue to run. Ignoring errors is a result we do not want to see, which will lead to difficulty in debugging the program. At this time, you can use the On Error GoTo 0 sentence to terminate the previous error processing, that is, you can appear in pairs with on error resume next. This will not affect the subsequent code.
Copy the codeThe code is as follows:
on error resume Next
。。。
'DB operate。。
。。。
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
On Error Goto 0
on error resume Next
。。。
'DB operate。。
。。。
If <>0 then
errNum =
errDesc =
Call G00B02logOut("Error.002", errNum ,errDesc )
End If
On Error Goto 0
There are several characteristics that need to be understood.
1. On error resume Next, if it is defined globally, the function is global. You use this sentence in the main program. If a function is called later, if there is an error in the function, it will be ignored. You can also catch this error after the statement called by the function in the main program. This can be verified by the following simple code:
Copy the codeThe code is as follows:
On error resume Next
funcb 'Call function
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
'On Error Goto 0
Sub funcb
aaaaaaaaaa'Invalid statement for testing
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End
On error resume Next
funcb 'Call function
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
'On Error Goto 0
Sub funcb
aaaaaaaaaa'Invalid statement for testing
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End
The result of executing the above code:
13 - The same type.
main ....
It can be seen that in the function, aaaaaaaaaaa is an error created on purpose, and the subsequent "funcb ok" and the subsequent code are not executed. However, the "main..." statement in the main program is executed. That is to say, if a statement in a function error occurs, the statements behind the function will not be executed, and the statements behind the statements that call the function are directly executed.
2. On error resume Next If defined in the function, see the execution of the following code
Copy the codeThe code is as follows:
funcb 'function call
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
Sub funcb 'function definition
On error resume Next
aaaaaaaaaaaa'Invalid statement
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End Sub
funcb 'function call
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
"main ...."
Sub funcb 'function definition
On error resume Next
aaaaaaaaaaaa'Invalid statement
"funcb ok"
If <> 0 Then
errNum =
errDesc =
errNum & " - " & errDesc
End If
End Sub
The execution results are as follows:
funcb ok
13 - The same type.
13 - The same type.
main ....
It can be seen that this error can be caught in the err object in the function body and the main program that calls it. This shows that the err object is global. It should be understood in this way 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 outside the function body.
The above are some experiences when using On error resume Next. If you understand the above two points, you can better use the error handling function.