You are on page 1of 2

Computer Programming 3 (Visual Basic Programming) 6.1 Simple Error Trapping 1. Start a new project.

Add a text box and a command button. 2. Set the properties of the form and each control: Form1: BorderStyle Caption Name Command1: Caption Default Name Text1: Name Text txtError [Blank] 1-Fixed Single Error Generator frmError Generate Error True cmdGenError

The form should look something like this:

3. Attach this code to the cmdGenError_Click event.


Private Sub cmdGenError_Click() On Error GoTo HandleErrors Err.Raise Val(txtError.Text) Err.Clear Exit Sub HandleErrors: Select Case MsgBox(Error(Err.Number), vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number)) Case vbAbort Resume ExitLine Case vbRetry Resume Case vbIgnore Resume Next End Select ExitLine: Exit Sub End Sub In this code, we simply generate an error using the number input in the text box. The generic error handler then displays a message box which you can respond to in one of three ways.

4. Save your application. Try it out using some of these typical error numbers (or use numbers found with online help). Notice how program control changes depending on which button is clicked. Error Number 6 9 11 13 16 20 52 53 55 61 70 92 Error Description Overflow Subscript out of range Division by zero Type mismatch Expression too complex Resume without error Bad file name or number File not found File already open Disk full Permission denied For loop not initialized

You might also like