<< Click to Display Table of Contents >> On Error (statement) |
On Error Goto Label
Or:
On Error Resume Next
Or:
On Error Goto 0
Enables an error handling routine for the handling of runtime errors:
▪On Error Goto Label indicates that in case of a runtime error execution should continue at the given target Label.
▪On Error Resume Next indicates that runtime errors are simply ignored. Attention: In this case, a runtime error can cause unpredictable results.
▪On Error Goto 0 disables error trapping – runtime errors cause the program to terminate with an error message.
An On Error statement is only valid inside the subroutine or function in which it resides.
If the script jumps to a label using the On Error Goto statement, you can resume execution at the calling place using the Resume statement. The script execution will then continue with the next line.
See also: Resume
Example:
In this example, an error is intentionally caused in order to execute the error handling routine at the label "Error". Then the user is asked whether the script's execution should be continued or not. If the answer is "Yes", execution will continue using the Resume Next command with the next line after the runtime error. If the answer is "No", execution ends with the Stop command.
Sub Main
On Error Goto MyErrorHandler
Print 1/0 ' Causes a "division by zero" error
MsgBox "End"
Exit Sub
MyErrorHandler: ' Error-handling routine
Dim DgDef, Msg, Response, Title
Title = "Error"
Msg = "A runtime error has been raised. Do you want to resume execution?"
DgDef = MB_YESNO + MB_ICONEXCLAMATION
Response = MsgBox(Msg, DgDef, Title)
If Response = IDYES Then
Resume Next
Else
Stop
End If
End Sub
For testing purposes, runtime errors can be artificially raised using the Err.Raise command.
Syntax: Err.Raise Number
Number is the number of a runtime error. There are the following runtime errors:
3: "RETURN without GOSUB"
5: "Invalid procedure call"
6: "Overflow"
7: "Out of memory"
9: "Subscript out of range"
10: "Array is fixed or temporarily locked"
11: "Division by zero"
13: "Type mismatch"
14: "Out of string space"
16: "Expression too complex"
17: "Can't perform requested operation"
18: "User interrupt occurred"
20: "RESUME without error"
28: "Out of stack space"
35: "Sub, Function, or Property not defined"
47: "Too many DLL application clients"
48: "Error in loading DLL"
49: "Bad DLL calling convention"
51: "Internal error"
52: "Bad file name or number"
53: "File not found"
54: "Bad file mode"
55: "File already open"
57: "Device I/O error"
58: "File already exists"
59: "Bad record length"
60: "Disk full"
62: "Input past end of file"
63: "Bad record number"
67: "Too many files"
68: "Device unavailable"
70: "Permission denied"
71: "Disk not ready"
74: "Can't rename with different drive"
75: "Path/File access error"
76: "Path not found"
91: "Object variable or WITH block variable not set"
92: "For loop not initialized"
93: "Invalid pattern string"
94: "Invalid use of NULL"
OLE Automation errors:
424: "Object required"
429: "OLE Automation server cannot create object"
430: "Class doesn't support OLE Automation"
432: "File name or class name not found during OLE Automation operation"
438: "Object doesn't support this property or method"
440: "OLE Automation error"
443: "OLE Automation object does not have a default value"
445: "Object doesn't support this action"
446: "Object doesn't support named arguments"
447: "Object doesn't support current local setting"
448: "Named argument not found"
449: "Argument not optional"
450: "Wrong number of arguments"
451: "Object not a collection"
Miscellaneous errors
444: "Method not applicable in this context"
452: "Invalid ordinal"
453: "Specified DLL function not found"
480: "ByRef parameter has the wrong type"