"On Error Resume Next" - Turning on Error Handling

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:

If a runtime error has occurred, use Err object properties to get more information about the error:

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:

<!-- runtime_error_handled.html
 - Copyright (c) 2002 HerongYang.com. All Rights Reserved.
<script language="vbscript">
   On Error Resume Next ' Turn on the error handling flag
   Call CheckError()

   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()
   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)
      document.writeln("There is no error at this time.")
   End If   
End Sub      

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:

Of course, this example script needs some enhancements to catch the first runtime error, not the last one.

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)


 Full Version in PDF/EPUB