This article was previously published under Q191474
This article has been archived. It is offered "as is" and will no longer be updated.
The Visual Basic ON ERROR statement is designed to encapsulate errorhandling for each procedure or module in an application. This behavior isby design, and is intended to conform with object oriented programming(OOP) conventions. When you create an application, you may wish to have ageneric error handler that traps all errors for the application, and handleexceptions to this routine as they occur in various modules.
The ON ERROR statement does not allow the use of an application wide ONERROR routine. However, it is possible to create a generic routine that iscalled from within each procedure or method in the application.
If the error routine is placed in a global module, it can be calledmanually from each procedure or method in your application. The followingcode example demonstrates how to set up a global error handling routine,and illustrates how this can be combined with specific error handling in aprocedure or method.
This example creates a project that contains two forms and one code module.Each form contains command buttons that cause errors to occur. Some errorsare handled within the click method for the command button. Other errorsare passed to the generic error handler.
Steps to Reproduce Behavior
Create a new standard EXE project in Visual Basic. Form1 is created by default.
Add the following code to the Load method of Form1. This code causes two errors to occur when the form is loaded. The first error will be handled by the form load method. The second error will be passed to the generic error handler:
Private Sub Form_Load() On Error GoTo FormLoadErr Err.Raise 76 Err.Raise 70 Exit Sub FormLoadErr: Select Case Err.Number 'Evaluate Error Number Case 76 MsgBox "Form_Load Error Handler. Form Does Not Exist" Case Else AppWideErr (Err.Number) 'Pass Error to generic module End Select End Sub
Add a CommandButton to the form. This code causes two errors to occur. The first error will be handled by the click method. The second error will be passed to the generic routine. Change the Caption property to "Cause Error 53 and 70." Add the following code to the Click method of the CommandButton:
Private Sub Command1_Click() On Error GoTo Cmd1Err Err.Raise 53 'Handled locally Err.Raise 70 'Handled by generic module Exit Sub Cmd1Err: Select Case Err.Number Case 53 MsgBox "Command 1 Error Handler" Case Else AppWideErr (Err.Number) End Select Resume Next 'Process the next (70) error End Sub
Add a second CommandButton to the form. If an error occurred in this method, no error handler would be called. Change the Caption property to "Show Form 2." Add the following code to the Click event:
Private Sub Command2_Click() 'No error handling is coded in this method 'AppWideErr would not be called Form2.Show 'Use VB's default error handling only End Sub
Add a second form to the project. Add a CommandButton to the form. This CommandButton passes all errors to the generic error handler and does not perform any special processing for specific errors. Change the Caption property of the button to "Cause Error 17." Add code to the Click method as follows:
Private Sub Command1_Click() On Error GoTo ThisSubErr Err.Raise 17 Exit Sub ThisSubErr: AppWideErr (Err.Number) End Sub
Add a module to the project. Add the following code to the module:
Public Sub AppWideErr(lnErrNumber) Select Case lnErrNumber 'Evaluate error passed to routine Case 70 'The following two lines of code should be typed on one line. 'Added title to MsgBox to make it clearer where error message 'came from. MsgBox "Generic Routine. Access Denied. See Net Administrator." , , "AppWideErr" Exit Sub Case Else 'The following two lines of code should be typed on one line. 'Added title to MsgBox to make it clearer where error message 'came from. MsgBox "Generic Routine. Unhandled Error: " + Err.Description + " # " & lnErrNumber , , "AppWideErr" Exit Sub End Select End Sub
Save and run the project. Note that some errors will be handled by the error routine specific to a method. The logic for each method invokes the generic AppWideErr procedure for any errors not explicitly handled by the method. The error number is passed as a numeric argument to AppWideErr.