∟"Err.Number" and "Err.Clear()" - Error Code and Clear Method
This section provides a tutorial example on how to manage runtime errors properly with the error code, Err.Number, and the error clear method, Err.Clear().
Remember the parsing_integer.html script example from the previous chapter?
It uses a couple of built-in functions to test the data type and value range
of a given string to avoid any runtime errors for data conversion task.
After learned how to manage runtime errors properly,
we are now ready to simply that VBScript example as:
<html>
<body>
<!-- parsing_integer_modified.html
- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
On Error GoTo 0
document.writeln(" ")
document.writeln("Converting variants to integers:")
document.writeln(" 777 = " & GetInteger(777))
document.writeln(" ""777"" = " & GetInteger("777"))
document.writeln(" 3.14159 = " & GetInteger(3.14159))
document.writeln(" ""3.14159"" = " & GetInteger("3.14159"))
document.writeln(" ""Hello"" = " & GetInteger("Hello"))
document.writeln(" True = " & GetInteger(True))
document.writeln(" Empty = " & GetInteger(Empty))
document.writeln(" Null = " & GetInteger(Null))
document.writeln(" ""1+2"" = " & GetInteger("1+2"))
document.writeln(" 777777 = " & GetInteger(777777))
document.writeln(" 3.3e200 = " & GetInteger(3.3e200))
document.writeln(" ""3.3e20000"" = " & GetInteger("3.3e20000"))
Function GetInteger(vAnyThing)
On Error Resume Next ' Turn on the error handling flag
GetInteger = CLng(vAnyThing)
If Err.Number > 0 Then
GetInteger = -2147483648
Err.Clear() ' The error is handled now. Remove it
End If
End Function
</script>
</pre>
</body>
</html>
Run this modified example code in IE, you will get the same result as the old example:
Notice that the "Err" object method "Clear()" is called to remove the runtime error,
once it has been caught and processed.
Otherwise, other code may get confused if the "Err" object still contains the error.