Err.Number Property

   
Err.Number Property

Class

Microsoft.VisualBasic.ErrObject

Syntax

 Err.Number 

Description

A read/write property containing a numeric value that represents the error code for the last error generated

Rules at a Glance

  • When a runtime error is generated within the program, the error code is automatically assigned to Err.Number.

  • The Number property is updated with an application-defined error whose code is passed as an argument to the Err.Raise method.

  • When using the Err.Raise method in normal code, your user -defined error codes cannot be greater than 65536 nor less that 0. (For an explanation, see the final note in Section of the Err.Raise Method entry.)

  • VB reserves error numbers in the range of 1-1000 for its own trappable errors. In addition, error numbers from 31001 to 31037 are also used for VB trappable errors. In implementing a series of application-defined errors, your error handlers should either translate application errors into VB trappable errors or, preferably, assign a unique range to application-defined errors.

  • When using the Err.Raise method in ActiveX objects, add the vbObjectError constant (-2147221504) to your user-defined error code to distinguish OLE errors from local-application errors.

  • When control returns to the local application after an error has been raised by the OLE server, the application can determine that the error originated in the OLE server and extract the error number with a line of code like the following:

     Dim lError as Long If (Err.Number And vbObjectError) > 0 Then     lError = Err.Number - ObjectError End If 

Programming Tips and Gotchas

  • An error code is a useful method of alerting your program that a function within an ActiveX or class object has failed. By returning a number based on the vbObjectError constant, you can easily determine that an error has occurred. ( vbObjectError is a constant that is defined in the Microsoft. VisualBasic.Constants class.) By then subtracting vbObjectError from the value returned by the object's function, you can determine the actual error code:

     If Err.Number < 0 then    Err.Number = Err.Number - ObjectError End If 
  • You can create a sophisticated multiresult error-handling routine by using the Err.Number property as the Case statement within a Select Case block, taking a different course of action for different errors, as this snippet demonstrates :

     Select Case Err.Number    Case < 0        'OLE Object Error       Set oObject = Nothing       Resume DisplayErrorAndExit    Case 5       'increment the retry counter and try again       iTries = iTries + 1       If iTries < 5 Then          Resume RetryFunctionCall       Else          Resume DisplayErrorAndExit       End If    Case 20       'we almost expected this one!       Resume Next    Case Else       Resume DisplayErrorAndExit End Select 
  • Directly assigning a Visual Basic-defined error code to the Number property does not automatically update the Description or other properties of the Err object.

See Also

Err.HelpContext Property, Err.HelpFile Property, Err.Source Property

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net