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:
"number" is an integer in the range of 0 and 65535 to represent a specific error condition.
But lower values are already used by VBScript for predefined errors.
So you should use higher values, like 60000, 60000, ...
"source" is a string to identify where the error occurred.
"description" is a string to describe the error condition.
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.