"On Error GoTo 0" - Turning off Error Handling

This section provides a tutorial example on how to use 'On Error GoTo 0' to turn off the error handling flag in a procedure to catch the first runtime error.

As you can see from the previous section, my last VBScript example reported the last runtime error, not the first one.

If you want to catch the first runtime error is a large section of code, you need to:

Here is the modified VBScript example to catch the first runtime error in a section of code:

<html>
<body>
<!-- runtime_error_caught.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()
   Call CodeToBeMonitored()
   Call CheckError()
   
Sub CodeToBeMonitored()
   On Error Goto 0    ' Turn off the error handling flag
      ' Exit on the first runtime error

   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")
End Sub

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 modified example code in IE, you will get:

There is no error at this time.

Before statement: x = 1/0

A runtime error has occurred:
   Err.Number = 11
   Err.Description = Division by zero
   Err.Source = Microsoft VBScript runtime error

What heppened was:

Now this example code behaves similar to "try ... catch" statement in some other language.

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

 PDF Printing Version