On Error

The On Error statement enables you to handle runtime errors.

Without an On Error statement, every runtime error leads to an error message, and the execution terminates.

Notes

  • An error handler is enabled by an On Error statement.

  • An active error handler is an enabled handler that is in the process of handling an error.

  • If an error occurs while an error handler is active, that is, between the occurrence of the error and a Resume, Exit Sub, Exit Function, or Exit Property statement, the error handler of the current procedure cannot handle the error.

    The control returns to the calling procedure. If the calling procedure has an enabled error handler, it is activated to handle the error.

    If the error handler of the calling procedure is also active, control passes back through the previous calling procedures until an enabled but inactive error handler is found.

    If no inactive, enabled error handler is found, the error is fatal at the point where it actually occurred.

  • Each time the error handler returns control to a calling procedure, that procedure becomes the current procedure.
  • When an error is handled by an error handler in any procedure, execution continues in the current procedure at the location specified by the Resume statement.
  • An error-handling routine is not a subprocedure or function procedure. It is a section of code identified by a line label or line number.

On Error GoTo [line]

If an error occurs, the processing continues at the specified label or line number and activates the error handler

On Error Resume Next

If an error occurs, the processing continues with the statement immediately following the statement that caused the runtime error, or with the statement immediately following the last call of the procedure containing the On Error Resume Next statement.

This allows the processing to continue despite a runtime error. You can then set up the error handling routine within the procedure.

On Error GoTo 0

This statement disables any enabled error handler in the current procedure.