Better Error Handling

Visual Basic automatically adds a form of error handling to your application which is good for developers, but not for end users. If an error occurs during a procedure your app will come with a message with error code and description, allowing the user to Debug or End. In the following I show what your error handling options are and how our Code VB centralised error handler will give you an excellent feedback about errors.

Add Error Handling

You can either add error handling using the builder to make a procedure, or you can add it afterwards using from the menu:

Alt-CEA. Use Alt-CEA[down-arrow] to open the combo box to show the full list to select the preferred errorhandler from.

The menu

Code-vb builders let you select the appropriate error handling.  In the Code-VB menu you will find under the 'Error' entry entries to speed up error handling.

  • Add error handling
  • Add / remove line numbers
  • Debug.Print statements for tracing during development
  • Additionally, error handling is supported as part of the builders

 

 

When considering error handling we have to match the possible error situations with the available error handling options:

1. Simple property Let/Get situation. - no error handling

Private msColorText As String
Public Property Get ColorText() As String
    ColorText = msColorText
End Property

I am quite certain we don't need error handling here. What could go wrong with assigning a string to a string variable?

vba default error message

VB default error message. We don't want users to see this!

 

 

 

 

 

 

 

. vb error handling

2. Don't really care - on error resume next

If your code does nice things like changing the status bar or setting hourglasses or disabling controls that are not critical to the main process you may decide in case of error you don't want to bother the user with minor errors.

3. Error occurs which can't be ignored - On Error GoTo HandleError

Supports a common technique of transferring control to section with code dedicated to handling the error:

Sub MySub()
Const cstrProcedure = "MySub"
On Error GoTo HandleError

HandleExit:

Exit Function

HandleError:
ErrorHandle Err, Erl(), cstrModule & "." & cstrProcedure
Resume HandleExit

If this is chosen additionally the module basErrorHandle with centralized error handling procedure ErrorHandle is inserted.

4. I expect an error here! - On Error GoTo HandleSelectCase

The well-known example here is of course trying to read a file. You may only hope it is there. There is a whole set of related and similar errors and handling them is probably something you want to do in the procedure in which the error occurs (e.g. retry/cancel), not in the centralized error handler.

On Error GoTo HandleError

HandleExit:

Exit Function
HandleError:
Select Case Err.Number
Case

Case Else
    ErrorHandle Err, Erl(), cstrModule & "." & cstrProcedure
    Resume HandleExit
End Select

5. No, I want to handle differently! - Do it yourself

OK, you probably know a better way. No problem, you can create your own error handling fragment using the Fragment Editor. As long as you store it in the \Fragments\ErrorHandlers subfolder it will automatically be added to the selection list.

Error reporting using mail

The best way to have errors handled is allowing users send you an email when the error occurs, automatically, with all relevant details (what error number, which procedure and line, additional info) included. This type of automated feedback let's you know how well your programs behave with minimum effort. You will get this reporting behaviour whenever you use Procedure Builder with default error handling.

mail error report

Customizing the centralized error handling procedure

The centralized error handling module is stored in \vbcode\ErrorHandlers\basErrorHandle.bas. You may choose to change it or make an alternative handler module and refer to that in dialog Settings / Files and Folders.

Locating errors module and procedure name strings

It is a common practice to make available not only the error info, but also where the error occurred. For this automatically Module and procedure name strings are added if they are needed. Again the name of these strings can be customized in the Settings dialog. If you add line numbers you can even identify which line the error occurred.

Add / remove line numbers

To add line numbers:

  1. Select the block to number
  2. Alt -CEU

To remove line numbers:

  1. Select the block to remove the numbers from
  2. Alt -CER

Add debug statements

The fast way to add debug statements:

  1. Alt -CED
  2. Pick the variable you want and press Ok