Classic Error Handling


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) - 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).




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net