In VBScript, three related to error handling are: On Error resume next, on error goto 0, and err.
1. Basic error messages
Dim I
I = 1 / 0
Result:
Microsoft VBScript runtime error '800a000b'
Division by zero
/Try. asp, Row 3
2. Prevent system error messages
When on error resume next is used, you can place a system error prompt and run it silently as if nothing had happened.
On Error Resume Next
Dim I
I = 1 / 0
Running result:
Null
3. Check System Errors
If the on error resume next is used, the last error message will be placed in the error ERR Object. An ERR Object has three important attributes: Number, source, and description. They are error numbers, error sources, and Error Descriptions. All you can capture are runtime errors, and if err then is equivalent to If err. number then.
On Error Resume Next
Dim I
I = 1 / 0
Execute " Test"
Response. Write err. Description
Running result:
Type Mismatch
We can see that the prompt is not divided by zero.
4. Let the system take over the handling of errors again
On Error goto 0, this statement will stop the system from being silenced. Once an error occurs, an error will be prompted and the script execution will end.
On Error Resume Next
Dim I
I = 1 / 0
Response. Write " 1"
On Error Goto 0
I = 1 / 0
Response. Write " 2 "
Running result:
1
Microsoft VBScript runtime error '800a000b'
Division by zero
/Try. asp, Row 7
We can see that the first 1 is output, and the second is not.
5. On Error resume next childProgramImpact
First look at the example
Sub Test ()
Dim I
I = 1 / 0
Response. Write " OK"
End sub
Sub Test1 ()
Test
Response. Write " OK"
End sub
On Error Resume Next
Test1
The running result is:
Null
Neither of the two OK columns is printed. Because on error resume next is the most complete issue, when a problem occurs in the called subroutine, the parts after the subroutine do not have the right to process, it is the most completeCodeWill jump out of the subroutine directly.
6. Impact of on error resume next on subprograms (2)
Let's look at the example.
Sub Test ()
On Error Resume Next
Dim I
I = 1 / 0
Response. Write " 3 "
End sub
Sub Test1 ()
Test
Response. Write " 2"
End sub
On Error Resume Next
Test1
Response. Write " 1 "
The execution result is:
3 2 1
It can be seen that after the on error resume next is used in test (), all the errors after this statement in this subroutine will be handled by themselves, but the ERR Object will not be cleared when it exits this function. In test1 and the underlying layer, the ERR Object can still be used to know the occurrence of the test () error.
7. Explanation of on error resume next behavior
A. After the on error resume next is executed, the "Unforgiven" Errors generated by all codes after this will not interrupt code execution.
B. If the on error resume next is not executed, all "Unforgiven" errors will cause the control process of the program to flow up to the layer. If it is at the bottom layer, the system prints the error.
C. The "Unforgiven" error leads to the flow going up the layer. If the upper-layer subroutine calls this subfunction after on error resume next, then this "Unforgiven" error becomes a "forgiveness" error and does not affect the process of the program.
D, on error resume next is actually limited to a sub-program, it can only ensure that after using this statement, the program processes in this subroutine are not affected by runtime errors. However, it does not affect the error handling logic of the upper and lower layers.
8. The behavior of on error goto 0 is the same as that of on error resume next, and its impact is limited to the subprogram.
Sub Test ()
On Error Goto 0
End sub
On Error Resume Next
Test
Dim I
I = 1 / 0
Running result:
Null
It can be seen that on error goto 0 in test () cannot bring its impact to the upper layer.
9. ERR Object assignment
The ERR Object will assign a new value when a running error occurs, and the old value will be discarded.
The ERR Object is not affected by function calls. It is completely global.
Err objects can be cleared using the clear method.
Err objects are cleared whenever on error resume next or on error goto 0 is called.
10. Summary
It can be seen that this error handling system can only be used to obtain information about running errors, which is inconvenient for individuals. Therefore, it is difficult to handle program Errors Based on such a weird system.