'error handling easy...

This code sample shows a simple way of expected exception handling for Visual Basic programs, similarly the Try-Catch statement in VB.net.


Supported Visual Basic versions:
  • Visual Basic 4, 5, 6
  • Visual Basic for Aplications (VBA): Excel, Access, Word, Outlook, PowerPoint, Publisher, Visio, AutoCad, etc.
  • VBScript

Advantages:
  • structured (no Goto jump), consistent and clear programming way to catch expected exceptions
  • cooporates with traditional procedure-level error trapping to handle unexpected run-time errors
  • not required variable declarations
  • can be copied or moved freely in the source code
  • can be nested (Try block in Try block)
  • supports recursive algorithms
  • exception handling is outside of the "On Error Resume Next" scope
  • hotkeys to insert Try-Catch block, Resume-Next block and Debugger template
  • ease and safe of use by a smart COM Add-In to insert code snipets
  • absolutely free

Restrictions:
  • Unfortunately there is no Finally clause (just like in C++).
  • After the "On Error Resume Next", only one statement can be used in the error testing section (between "On Error Resume Next"  and  ".Catch").
    If you want to use more statements to handle unexpected run-time errors, then you can use the traditional error handling with the "On Error GoTo" statement (download samples).
  • VBScript:  the "Case 0" clause is required when "Case Else" clause is used because the "Is" keyword is not impelemented in the Select-Case statement in VBScript.

Download... (Includes samples and free portable VB6-VBA COM Add-Ins with source code, etc...)
  • License: Freeware
  • COM Add-In version: 1.100517
  • Size: 277 KB
  • Release Date: May 18, 2010
  • Author: Robert Einhorn
E-mail


What's new:
  • May 17, 2010 (v1.100517):
        - A bug fixed, when "Option Explicit" is disabled
        - Debugger template extended with logging features
        - Some sample codes modified

  •     January 04, 2010 (v1.100104):
        - Hotkey support for VB IDE:
            Alt+Y    insert empty Try-Catch block
            Alt+N    insert empty Resume-Next block
            Alt+G    insert Debugger template for runtime errors



VBScript example:
'*** programmed by Robert Einhorn, Budapest, 2008 ***
Option Explicit

Dim nTest

With New Try: On Error Resume Next
    nTest = 1 / 0 'Division by zero
.Catch: On Error GoTo 0: Select Case .Number
    Case 11
        MsgBox .Description
End Select: End With


Class Try
    Private mstrDescription
    Private mlngHelpContext
    Private mstrHelpFile
    Private mlngNumber
    Private mstrSource

    Public Sub Catch()
        mstrDescription = Err.Description
        mlngHelpContext = Err.HelpContext
        mstrHelpFile = Err.HelpFile
        mlngNumber = Err.Number
        mstrSource = Err.Source
    End Sub

    Public Property Get Source()
        Source = mstrSource
    End Property
   
    Public Property Get Number()
        Number = mlngNumber
    End Property
   
    Public Property Get HelpFile()
        HelpFile = mstrHelpFile
    End Property
   
    Public Property Get HelpContext()
        HelpContext = mlngHelpContext
    End Property
   
    Public Property Get Description()
        Description = mstrDescription
    End Property
End Class



VB6 and VBA COM Add-In screenshots:

Inserting empty Try-Catch block.



Cooporating with traditional procedure-level error trapping to catch unexpected run-time errors.




Debugger for unexpected run-time errors.


Comments