This appendix provides information on error handling.
A Try block tries to execute some code and reacts to errors. The syntax is as follows:
Trytry
_statements
... [Catch ex Asexception
_type
_1
exception
_statements
_1
...] [Catch ex Asexception
_type
_2
exception
_statements
_2
...] ... [Catchfinal
_exception
_statements
...] [Finallyfinally
_statements
...] End Try
When an error occurs, the program examines the Catch statements until it finds one that matches the current exception. The program executes the finally_statements after the try_statements succeed or after any Catch block is done executing.
Use the Throw statement to throw an exception, as in the following code:
Throw New ArgumentException("Width must be greater than zero")
Exception classes provide several overloaded constructors so you can indicate such things as the basic error message, the name of the variable that caused the exception, and an inner exception.
For information on useful exception classes and custom exception classes, see Appendix O, "Useful Exception Classes."
The On Error statement controls error handlers in Visual Basic classic error handling. You can use structured and classic error handling in the same program but not in the same routine.
The following list briefly describes the On Error statement's four variations:
On Error GoTo
line — If an error occurs, the program enters error-handling mode and control jumps to the indicated line.
On Error Resume Next
— If an error occurs, the program ignores it. The code can use the Err object to see whether an error occurred and what error it was.
On Error GoTo 0
— This command disables any currently active error handler. If the program encounters an error after this statement, the routine fails and control passes up the call stack until the program finds an active error handler or the program crashes.
On Error GoTo −1
— This command is similar to On Error GoTo 0, except that it also ends error-handling mode if the program is in error-handling mode.
Visual Basic provides four ways to exit error-handling mode:
Exit Sub
(or Exit Function
or Exit Property
) — Ends error-handling mode and exits the current routine.
Resume
— Makes the program resume execution with the statement that caused the error. If the program has not taken some action to correct the problem, the error will occur again, triggering the error handler and possibly entering an infinite loop.
Resume Next
— Makes the program resume execution with the statement after the one that caused the error.
On Error GoTo −1
— Ends error-handling mode and lets execution continue with the statement that follows.
Using On Error GoTo −1 to end error-handling mode can be very confusing because it's hard to tell when the program is in error-handling mode and when it isn't. Usually, the code is easier to understand if all of the error-handling code is grouped at the end of the routine and if each block of error-handling code ends with one of the other methods (Exit Sub, Exit Function, Resume, or Resume Next).