Classes and Err Object

Discussion in 'AutoCAD' started by Danny P., Feb 20, 2004.

  1. Danny P.

    Danny P. Guest

    All,

    I'm struggling with handling errors within my own class modules. I was
    considering the Err object, but I'm not able to error trap it.

    As an example, let's say I have a Layers class and a Layer class. Within
    the Layer class is code for assigning a color. You can't assign a string to
    it because it's expecting a number from 1-255. So, I use
    Err.Raise 1, "Not a number between 1-255."
    when it receives a string.

    In the Layers class, I have an AddLayer function which creates a layer given
    a name and a color. So something like:
    Layers.AddLayer "MyLayer", 7
    will work, but
    Layers.AddLayer "MyLayer", "A Color"
    will raise the error.

    This is working the way I expect. However, I can't use this:

    On Error Resume Next
    Layers.AddLayer "MyLayer", "A Color"
    If Err.Number = 1 Then
    Err.Clear
    MsgBox "You moron, you need a number"
    End If

    The error still raises, and the "On Error Resume Next" statement is ignored.
    Obviously, I could add test statements in the Layers class to make sure it's
    passing a number, but I want the Layer class to be self-sufficient by
    accepting all parameters, then raising errors for bad ones.

    Am I doing something wrong, or is there a better way to handle errors within
    my classes?

    Thanks in advance for your help,
    -Danny Polkinhorn
    WATG
    Honolulu
     
    Danny P., Feb 20, 2004
    #1
  2. Danny P.

    Norman Yuan Guest

    In VBA IDE, click "Tools->Options...", then "General" tab, in "Error
    Trapping" frame, make sure "Break on Unhandked Error" is selected (it should
    be default selection.

    It is often helpful to set it to "Break on All Errors" while you are
    developing/testing/degugging. But do remember set is back to "Break on
    Unhandled Errors" before put your VBA project to production.
     
    Norman Yuan, Feb 20, 2004
    #2
  3. Danny P.

    Danny P. Guest

    Norman,

    Thanks for the reply. My error trapping settings are already that way. It
    seems however that I can't trap errors that I raise within a different class
    using the Err object. VBA errors are trappable, but Err object errors are
    not (at least that's what I'm experiencing).

    -Danny
     
    Danny P., Feb 20, 2004
    #3
  4. Danny P.

    Norman Yuan Guest

    Let me make it straight:

    On your first post, you said the error IS raised, but not trappable, i.e.

    On Error Resume Next
    Layers.AddLayer "MyLayer", "A Color" '''DOES your program stops here?
    If Err.Number = 1 Then
    Err.Clear
    MsgBox "You moron, you need a number"
    End If

    Try this:

    On Error Resume Next
    Layers.AddLayer "MyLayer", "A Color"
    If Err.Number <>0 Then
    MsgBox Err.Number & vbCr & Err.Description 'Show the error's
    number to see if it is the number you defined
    'Err.Clear
    'MsgBox "You moron, you need a number"
    End If

    Also, why not write a small test routine to make sure your Acad VBA works
    properly:

    In Acad VBAIDE, add a class: Class1

    Option Explicit

    Public Sub Test()
    Err.Raise 33333, "Class1", "Error occurs here!"
    End Sub

    In a new module, add following macro

    Sub ErrorTest()

    Dim cls As Class1
    Set cls = New Class1

    On Error Resume Next

    cls.Test

    If Err.Number <> 0 Then
    MsgBox "Error raised: " & vbCr & Err.Source & vbCr &
    Err.Description, vbInformation
    Else
    MsgBox "No Error raised", vbInformation
    End If


    End Sub

    The raised error is captured perfectly.

    So, it must be something not right in your code. If you post the code where
    error is raised, the thing may be easier to be located.
     
    Norman Yuan, Feb 20, 2004
    #4
  5. Danny P.

    Danny P. Guest

    Norman,

    Thanks for the reply and your help. I hope you're still watching this
    thread. Try this:

    Use your Class1
    Create a new class, "Class2"
    Put your ErrorTest code there

    Now in a module, put this code:

    Sub ModuleErrorTest()

    Dim clsTwo As new Class2
    clsTwo.ErrorTest
    Set clsTwo = Nothing

    End Sub

    Now if you run the module code, the error is raised and not trapped by
    Class2. Any workaround, or am I making incorrect conceptual assumptions? I
    really want Class2 to handle any errors because of the depth of the 'tree'.
    I don't want to put error trapping at the module level in this example.

    Thanks again,
    Danny Polkinhorn
    WATG
    Honolulu
     
    Danny P., Feb 26, 2004
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.