Whenever your program interacts with the outside world, you should provide some form of error handling to counteract unexpected inputs or outputs. One way of providing error handling is to write your own error-handling routines.
Error-handling routines are the traffic control for your program. Such routines can handle any kind of programming or human-generated errors you can think of. They should not only identify the error, but try to fix it-or at least give the program or interacting human a chance to do so.
To begin error handling in a procedure, use the On Error GoTo statement to signify that you are going to use an error-handling routine:
On Error GoTo ErrorHandler
This statement can go anywhere in your procedure, but should be placed toward the top, generally right after any procedure-level variable declarations.
Error-handling routines can be given any name: ErrorBin, ErrorBucket, or whatever you like.
The keyword GoTo is a carryover from an old programming practice made popular in various languages such as BASIC and COBOL. A GoTo was regularly used for designing and building modularized programs. To break programs into manageable pieces, programmers would create modules and link them together using the keyword GoTo.
After years of programming with GoTo, programmers began to realize that this created messy "spaghetti-like" code, which at times became nearly impossible to debug. Fortunately, eventdriven and object-oriented programming techniques have virtually eliminated the use of GoTo.
Once an error handler has been declared, errors generated in the procedure are directed to the error-handling routine, as demonstrated in this example.
Public Function Verify_Input() As Boolean On Error GoTo ErrorHandler 'get Input from user Exit Function ErrorHandler: MsgBox ("An error has occurred.") Resume End Function
It is necessary to execute the Exit Function or Exit Sub statements before program execution enters the error-handling routine. Without these statements, a procedure that executes without errors executes the error handler as well. That's an important note, so let me repeat it again in italics: Without an Exit statement, a procedure that executes without errors executes the errorhandling routine as well.
Error handling begins by typing the name of the error handler followed by a colon. Within the error handler, you write code to respond to the error. In the previous example, simply use a message box to report that an error has occurred.
The Resume keyword takes program execution back to the statement where the error occurred. Note that there are three possible ways for returning program control to the procedure:
- Resume. By itself, the keyword Resume returns program control to where the error occurred.
- Resume Next. The Resume Next statement returns program control to the statement after the statement where the error occurred.
- Resume Label. The Resume Label statement returns program control to a predetermined line number, as seen in the following code.
Public Function Verify_Input() As Boolean On Error GoTo ErrorHandler 'get Input from user BeginHere: Exit Function ErrorHandler: MsgBox ("An error has occurred.") Resume BeginHere: End Function
Generally speaking, message boxes are good ways to let a user know an error has occurred. However, knowing that an error has occurred is not enough; the user also needs to know what caused the error and possible solutions to resolve the error.
In the next section, you learn how to identify specific and custom errors using the Err object.