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

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

  • 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) 2015, HerongYang.com, All Rights Reserved.
-->
<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.

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

"On Error Resume Next" - Turning on Error Handling - Updated in 2015, by Dr. Herong Yang