The Err Object


When an error occurs, Visual Basic initializes an object named Err. You can use this object’s properties to learn more about the error. These properties correspond to those provided by the exception objects used by the Try statement’s Catch sections. The following table lists these properties.

Open table as spreadsheet

Property

Purpose

Description

A message describing the error.

Erl

The line number at which the error occurred.

HelpContext

The help context ID for the error.

HelpFile

The full path to the help file describing the error.

LastDLLError

A system error code generated by a call to a DLL (if appropriate).

Number

The error number. The value 0 means no error has occurred.

Source

The name of the object or application that caused the error.

The Err object also provides three useful methods for working with errors: Clear, Raise, and Get?Exception. The Clear method clears the object’s information and resets it for the next statement. If the statement following an error does not raise an error itself, the Err object may still show the previous error unless you clear it, as shown in the following code:

  On Error Resume Next X = Single.Parse(txtX.Text) If Err.Number <> 0 Then     MessageBox.Show(Err.Description)     ' Display the error.     Err.Clear                            ' Clear the error. End If Y = Single.Parse(txtY.Text) If Err.Number <> 0 Then     MessageBox.Show(Err.Description)     ' Display the error.     Err.Clear                            ' Clear the error. End If ...  

The Err object’s Raise method generates an error. For example, the following statement raises error number 5, “Procedure call or argument is invalid”:

  Err.Raise(5) 

Finally, the GetException method returns an exception object representing the Err object’s error. You can use this object just as you can use any other exception object. In particular, you can use its StackTrace property to get a trace starting where the error occurred.

If you use classic error handling, you can use the Err object to learn about the error. If you use structured error handling with the Try statement, you can use the exception objects provided by Catch statements, and you can do without the Err object.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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