http://www.herongyang.com/VBScript/About-VBScript-Tutorials-Book.html
http://www.herongyang.com/VBScript/Error-Handling-On-Error-Resume-Next.html
|
This section provides a tutorial example on how to use the 'On Error Resume Next' statement to turn on error handling flag. You can use Err.Number > 0 to test if there is any runtime error has been raised or not. |
We have seen what happens when the error handling flag is turned off in the previous section. Now let's see how the "On Error Resume Next" statement should be used:
- By default, the error handling flag is turned off.
- You can turn on the error handling flag at time your want by entering the "On Error Resume Next" statement.
- Once the error handling flag is turned on, execution will not be stopped when a runtime error occurs.
- You can use the condition of (Err.Number>0) to determine a runtime error has occurred or not.
If a runtime error has occurred, use Err object properties to get more information about the error:
- Err.Number - "Err" object property containing the error code.
- Err.Description - "Err" object property containing the error description.
- Err.Source - "Err" object property containing error source identification.
I have modified the VBScript example used in the previous section to try to check the "Err" object by myself with the error handling flag turned on:
<html>
<body>
<!-- runtime_error_handled.html
Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
On Error Resume Next ' Turn on the error handling flag
Call CheckError()
document.writeln("")
document.writeln("Before statement: x = 1/0")
x = 1/0 ' Division by zero
document.writeln("Before statement: y = CInt(777777)")
y = CInt(777777) ' Overflow
document.writeln("Before statement: z = 1 + ""2nd""")
z = 1 + "2nd" ' Type mismatch
document.writeln("End of test")
Call CheckError()
Sub CheckError()
document.writeln()
If Err.Number > 0 Then
document.writeln("A runtime error has occurred:")
document.writeln(" Err.Number = " & Err.Number)
document.writeln(" Err.Description = " & Err.Description)
document.writeln(" Err.Source = " & Err.Source)
Else
document.writeln("There is no error at this time.")
End If
End Sub
</script>
</pre>
</body>
</html>
Run this example code in IE, you will get:
There is no error at this time. Before statement: x = 1/0 Before statement: y = CInt(777777) Before statement: z = 1 + "2nd" End of test A runtime error has occurred: Err.Number = 13 Err.Description = Type mismatch Err.Source = Microsoft VBScript runtime error
The output confirms that:
- When CheckError() was called at the beginning, Err.Number is 0, indicating that there is no runtime error.
- When the first runtime error occurred on statement, x = 1/0, execution continued because the error handling flag was turned on.
- When the second runtime error occurred on statement, y = CInt(777777), execution continued again because the error handling flag was turned on.
- When the third runtime error occurred on statement, z = 1 + "2nd", execution continued again because the error handling flag was turned on.
- When CheckError() was called at the end, Err.Number is 13, indicating that the last runtime error occurred was a "Type mismatch" error.
Of course, this example script needs some enhancements to catch the first runtime error, not the last one.
浙公网安备 33010602011771号