Description
Returns error status.
Syntax
Err
Erl
Remarks
After an error occurs, the Err function returns a number that is the run-time error number, identifying the error. The Erl function returns a number that is the line number of the line in which the error occurred, or the numbered line most closely preceding it.
Because Err and Erl return meaningful values only after an error has occurred, they are usually used in error-handling routines to determine the error and corrective action. Both Err and Erl are reset to 0 after any form of the Resume or On Error statement and after an Exit Sub or Exit Function statement within an error-handling routine.
Caution
If you set up an error handler using On Error GoTo and that error handler calls another procedure, the value of Err and Erl may be reset to 0. To make sure that the value doesn't change, assign the values of Err or Erl to variables before calling another procedure or before executing Resume, On Error, Exit Sub, Exit Function, or Exit Property.
You can directly set the value returned by the Err function using the Err statement. You can set values for both Err and Erl indirectly using the Error statement.
The Erl function returns only a line number, not a line label, located at or before the line producing the error. Line numbers greater than 65,529 are treated as line labels and can't be returned by Erl. If your procedure has no line numbers, or if there is no line number before the point at which an error occurs, Erl returns 0.
See Also
Err Statement, Error Function, Error Statement, On Error Statement, Resume Statement.
Example
This example shows an error-handling routine which uses the Err and Erl functions. If there are no additional errors, Err returns error number 11 and Erl returns line 1030.
Sub ErrDemo() 1010 On Error GoTo ErrorHandler ' Set up an error handler. 1020 B = 1: C = 0 ' Initialize variables. 1030 A = B \ C ' Cause a "Division by zero" error. 1040 Exit Sub ErrorHandler: ' Error handler. ErrorNumber = Err ' Get run-time error number. ErrorLine = Erl ' Get line number. Resume Next ' Resume execution at next line. End Sub