Microsoft Visual Basic for Applications 7.1; Version 1088
I created a custom error handler by following online tutorials and I've been using it for about a year now but I still don't get the part where vbObjectError
constant is added to a number between 513-65535, which is reserved for user-defined errors. Most tutorials would recommend to use Err.Raise vbOjectError + 1000
as an example to generate custom errors. The reason for this is to avoid overlapping with errors 0-512, which is reserved for system errors. If I have to write a code around that idea, the code would look like this:
Option Explicit
Sub raiseError()
On Error GoTo errorHandler
Dim x As Double
Dim y As Double
Let x = 4.8
Let y = 5.5
If x <> y Then
Err.Raise vbObjectError + 1000
End If
errorHandler:
Select Case Err.Number
Case vbEmpty
MsgBox "alright!"
Case vbObjectError + 1000
MsgBox ("User-defined error '" & Err.Number & "':" & _
vbNewLine & _
vbNewLine & _
"X is not equal to Y")
Case Is <> vbObjectError + 1000
MsgBox "All other errors"
End Select
End Sub
Now, quoting Microsoft from this documentation:
"Visual Basic errors (both Visual Basic-defined and user-defined errors) are in the range 0–65535. The range 0–512 is reserved for system errors; the range 513–65535 is available for user-defined errors.
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."
But what confuses me is vbObjectError
constant has a value of -2147221504
. As you can see if you run the code, the sum of vbObjectError
and 1000
or any number between 513–65535
is far from the range 513–65535, which is available for user-defined errors according to Microsoft.
If I need to use error numbers in the range of 513–65535
, why not use those numbers directly like Err.Raise 513
or Err.Raise 1000
?
I'd really appreciate any clarifications from you guys. Thank you all very much.
I finally found a good answer to this question. The Microsoft documentation for Err.Number explains it well and provides this example:
' Using Number property with an error from an
' Automation object
Dim MyError, Msg
' First, strip off the constant added by the object to indicate one
' of its own errors.
MyError = Err.Number - vbObjectError
' If you subtract the vbObjectError constant, and the number is still
' in the range 0-65,535, it is an object-defined error code.
If MyError > 0 And MyError < 65535 Then
Msg = "The object you accessed assigned this number to the error: " _
& MyError & ". The originator of the error was: " _
& Err.Source & ". Press F1 to see originator's Help topic."
' Otherwise it is a Visual Basic error number.
Else
Msg = "This error (# " & Err.Number & ") is a Visual Basic error" & _
" number. Press Help button or F1 for the Visual Basic Help" _
& " topic for this error."
End If
MsgBox Msg, , "Object Error", Err.HelpFile, Err.HelpContext
The key is that you are expected to subtract the large negative number from Err.Number and do your Select Case
on the result. 0 - 65535 means the error was generated by Err.Raise
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With