0

Previous version of the similar question: Error handling in class returns runtime error 440 "automation error"

There's a class ClsSubject. It has a property called INN. When letting a value into .INN the class checks wheither:

1) the value is a numeric string and

2) the value is exactly 10 characters long.

Otherwise it throws an error. Here's the code of class module:

Option Explicit
Private PINN As String

Property Let INN(val As String)
    If IsNumeric(val) And Len(val) = 10 Then

        PINN = val
    Else

        Dim ErrorNum As Long
        ErrorNum = 513

        Dim MessageDescripton As String
        MessageDescripton = val & " is not a valid value"

        Err.Raise ErrorNum, "ClsSubject", MessageDescripton
    End If
End Property

Property Get INN() As String
    INN = PINN
End Property

The debugging code in the main module:

Sub Test()
    Dim I As New ClsSubject
    I.INN = 99
End Sub

Expected outcome: Error 513 "99 is not a valid value"

Actual outcome: Error 513 "Application-defined or Object-defined error"

P.S. I tried to change the error number of error by adding vbObjectError value to it like this:

ErrorNum = vbObjectError + 513

But this gives me Runtime error -2147220991 (80040201) Automation Error: An even was unable to invoke any of the subscribers

P.P.S.: Since the question is getting no-repro, here's an additional information. The code is written in MS Excel 2010 standard, version 14.0.6023.1000 (32 bit). The workbook contains no code other than mentioned above. The screenshots:

The main module code Class module code

Igor Cheglakov
  • 525
  • 5
  • 11
  • 1
    No-repro. Shows "Error 513: 99 is not a valid value". – GSerg Jan 13 '20 at 19:17
  • 3
    Previously - https://stackoverflow.com/questions/59705652/error-handling-in-class-returns-runtime-error-440-automation-error Please reference previous instances of the same question so we can save time by not giving answers you already got... – Tim Williams Jan 13 '20 at 19:26
  • Have you tried throwing error 5 with a custom message instead? Error 5 is essentially VBA's [ArgumentException](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception?view=netframework-4.8), which is pretty appropriate for such guard clauses. – Mathieu Guindon Jan 13 '20 at 19:41
  • 1
    Side note, `Dim ... As New` should be avoided, it has undesirable side-effects. – Mathieu Guindon Jan 13 '20 at 19:43
  • 1
    Possible duplicate of [Err.Raise() is ignoring custom description and source](https://stackoverflow.com/q/31234805/11683) – GSerg Jan 13 '20 at 19:44
  • 1
    tl;dr: Caused by the [Break on unhandled errors](https://stackoverflow.com/q/12687105/11683) error trapping mode. – GSerg Jan 13 '20 at 19:45
  • @Mathieu Guindon I intended to make my error more clear to a user e.g. "Invalid Value: 123. Value must be a 10-digit string". – Igor Cheglakov Jan 13 '20 at 19:56
  • The error number is meaningless for the user, it's meant for the dev. The message can be whatever you make it. I do this all the time: `Err.Raise 5, TypeName(Me), "Invalid value: " & val & ". Value must be a 10-digit string."` - that said `Val` is the name of a function in the VBA standard library, that you are shadowing in that procedure scope. Consider using a better, more descriptive name that isn't already in scope. – Mathieu Guindon Jan 13 '20 at 20:01
  • I can't get a custom description that's the question. All I get is "Runtime error -2147220991 (80040201) Automation Error: An even was unable to invoke any of the subscribers" if I add vbObjectError to my error code or "Application-defined or Object-defined error" if I don't – Igor Cheglakov Jan 13 '20 at 20:05
  • 1
    Have you not seen @GSerg's comment above? Verify Tools > Options > "Break on unhandled errors" – Mathieu Guindon Jan 13 '20 at 20:14

1 Answers1

2

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/raise-method

When setting the Number property to your own error code in a class module, you add your error code number to the vbObjectError constant. For example, to generate the error number 513, assign vbObjectError + 513 to the Number property.

Err.Raise vbObjectError + ErrorNum, "ClsSubject", MessageDescripton
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • 1
    On the other hand, raising [error 5](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/invalid-procedure-call-or-argument-error-5) may be more appropriate. – GSerg Jan 13 '20 at 19:12
  • 1
    Yes, by the way I forgot to mention. I tried summing my number with `vbObjectError.` It throws `Runtime error -2147220991 (80040201) Automation Error: An even was unable to invoke any of the subscribers` – Igor Cheglakov Jan 13 '20 at 19:13