VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"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:

Converting variants to integers:
   777 = 777
   "777" = 777
   3.14159 = 3
   "3.14159" = 3
   "Hello" = -2147483648
   True = -1
   Empty = 0
   Null = -2147483648
   "1+2" = -2147483648
   777777 = 777777
   3.3e200 = -2147483648
   "3.3e20000" = -2147483648

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.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

Error Handling Flag and the "Err" Object

 Error Handling Rules Overview

 IE Option Setting - Enable Script Debugging

 "On Error Resume Next" - Turning on Error Handling

 "On Error GoTo 0" - Turning off Error Handling

"Err.Number" and "Err.Clear()" - Error Code and Clear Method

 Built-in "Err" Object Properties and Methods

 "Err.Raise()" - Raising Your Own Errors

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"Err.Number" and "Err.Clear()" - Error Code and Clear Method - Updated in 2015, by Dr. Herong Yang