The Number property of the Err object identifies an error by number. It is
set by Visual Basic when a runtime error occurs. It can also be set in code by a developer to
identify a user-defined error. The use of the Err object for user-defined errors are described
later in this chapter (see
"Sending
Information from a COM Component"
in
Chapter
12).
When a runtime error occurs, the Err.Number property can be used to determine
what that error was and how it should be handled. Some error numbers that Windows generates are
described later in this chapter in the section titled "Handling Errors
in Code."
The Number property contains a long integer value. Visual Basic error numbers
and "user-defined" numbers (that is error numbers implemented by the programmer)
range from 0 to 65,535. If you define any of your own errors in your application,
use numbers below the 65,535 limit specified by Visual Basic. Also, be careful
not to use a number already defined by Visual Basic. VB reserves numbers up through
512 for itself, so the practical range available to you for your own "user-defined"
errors is 513-65,535 (see "Trappable Errors" later in this
chapter).
VB documentation further recommends that you add the constant vbObjectError to your
error code so that calling routines can definitely identify your error as other than a standard
VB error. Microsoft is preparing us for the day when they will need to use errors between
513 and 65,535.
IMPORTANT - Numeric Range For Your Own Error Numbers:
Implementing userdefined errors with numbers in the range between 0 and 512 (the
range reserved for VB-defined errors) will cause confusion for other developers
working with your projects. If you accidentally use an error number that already
has meaning to Visual Basic, another developer will not know whether the error
should be treated as a user-defined error or as a standard error. Even if you
use a number in the range that is not defined in the current release of Visual
Basic (513-65,535), it might be implemented in subsequent releases. To avoid confusion,
it is best to implement user-defined error numbers in the 513 through 65,535 range,
adding the constant offset amount vbObjectError to your error number as discussed
above and in the section in this chapter devoted to vbObjectError.