The Error Class Revisited

In the comments to Error Handling Via an Error Class, Peter found that the problem with this method is the absence of the Stop and Resume in the error handler than let you debug at the line that caused the error. Yeah, that stinks. Then Jase got me thinking that I just wouldn’t create the class in debug mode. Well, that wasn’t quite right. What needed to happen was that the error handler should not be set in debug mode. Here’s a rewrite of the entry point procedure.

Sub EntryPoint()
    Dim clsError As CError
    gbDebugMode = False
    If Not gbDebugMode Then On Error GoTo ErrHandler
    Set clsError = New CError: clsError.SetLoc "Module1", "EntryPoint"
    Exit Sub
    Set clsError = Nothing
    MsgBox Err.Description
    Resume ErrExit
End Sub

When gbDebugMode is False, the error handler is set and it works as described in the original post. That is, the user gets a message box and the code exits gracefully. When gbDebugMode is True, the error handler is not set. It’s like you don’t have an error handler at all – because you don’t. When in debug mode, you get kicked to the line that caused the error.

Is that that last hurdle?


  1. Alex Godofsky says:

    It seems like Microsoft could save everyone a whole lot of effort by updating VBA to C# or VB.NET.

  2. Peter Albert says:

    I like the idea! Will implement in my next project and get back to you with any feedback! :-)

  3. Jase says:

    SIMPLE! SIMPLE! and Elegant. I love this and think you nailed it this time.

  4. David says:

    Note, you can achieve a similar result with Tools | Options in the Visual Basic editor. Just set the option to break on all errors. I frequently do this to defeat my error trapping when I want to look for a bug.

Posting code or formulas in your comment? Use <code> tags!

  • <code lang="vb">Block of code goes here</code>
  • <code lang="vb" inline="true">Inline code goes here</code>
  • <code>Formula goes here</code>

Leave a Reply

Here's how to update your reports of company and nearly any web data: