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

This chapter provides tutorial examples and notes on runtime error handling. Topics include the default error handling behavior; 'On Error Resume Next' and 'On Error GoTo 0' statements; catching errors with (Err.Number > 0); clear Err object with Err.Clear(); raise your own errors with Err.Raise(...).

In previous sections, we learned that the "Err" is used the host environment to raise pre-defined runtime errors.

VBScript also allows you to use the "Err" to raise your own runtime error with the Err.Raise() method:

   Call Err.Raise(number, source, description)

where:

To show you how the Err.Raise() method works, I wrote the following VBScript example,

<html>
<body>
<!-- runtime_error_raised.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   On Error Resume Next

   document.writeln("Validating ""1999-12-31""...")
   Call ValidateIsoDate("1999-12-31")
   Call CheckError()

   document.writeln("Validating ""Year-12-31""...")
   Call ValidateIsoDate("Year-12-31")
   Call CheckError()

   document.writeln("Validating ""2006-15-31""...")
   Call ValidateIsoDate("2006-15-31")
   Call CheckError()

   document.writeln("Validating ""1999/12/31""...")
   Call ValidateIsoDate("1999/12/31")
   Call CheckError()

'  Validating date string in ISO format: yyyy-mm-dd
Sub ValidateIsoDate(sString)
   If Len(sString) <> 10 Then 
      Call Err.Raise(60001, "My test", "Length must be 10")
      Exit Sub
   End If

   sYear = Mid(sString, 1, 4)
   If Not IsNumeric(sYear) Then
      Call Err.Raise(60002, "My test", "Year must be a number")
      Exit Sub
   End If

   sDash = Mid(sString, 5, 1)
   If sDash <> "-" Then
      Call Err.Raise(60003, "My test", "Missing the first dash")
      Exit Sub
   End If

   sMonth = Mid(sString, 6, 2)
   If Not IsNumeric(sMonth) Then
      Call Err.Raise(60004, "My test", "Month must be a number")
      Exit Sub
   End If
   If CInt(sMonth) < 1 or CInt(sMonth) > 12 Then
      Call Err.Raise(60005, "My test", "Invalid month")
      Exit Sub
   End If

   sDash = Mid(sString, 8, 1)
   If sDash <> "-" Then
      Call Err.Raise(60006, "My test", "Missing the second dash")
      Exit Sub
   End If

   sDate = Mid(sString, 9, 2)
   If Not IsNumeric(sDate) Then
      Call Err.Raise(60007, "My test", "date must be a number")
      Exit Sub
   End If
   If CInt(sDate) < 1 or CInt(sDate) > 31 Then
      Call Err.Raise(60008, "My test", "Invalid date")
      Exit Sub
   End If
End Sub

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

Running this script example in IE, you will get:

Validating "1999-12-31"...
There is no error at this time.
Validating "Year-12-31"...
A runtime error has occurred:
   Err.Number = 60002
   Err.Description = Year must be a number
   Err.Source = My test
Validating "2006-15-31"...
A runtime error has occurred:
   Err.Number = 60005
   Err.Description = Invalid month
   Err.Source = My test
Validating "1999/12/31"...
A runtime error has occurred:
   Err.Number = 60003
   Err.Description = Missing the first dash
   Err.Source = My test

The output shows that I did a good job of validating a date string in ISO format. I was able to validate 7 error conditions and raise a different runtime error for each condition. Of course, there are more error conditions left for you to add.

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