Err Function

Returns the error number corresponding to the most recent run-time error.

Syntax

Err()

Remarks

The Err function is normally used to determine the number of an occured run-time error. Usually it may be needed in an error handler defined by the On Error statement.

Example

This example shows how the Err function is used in the ErrorHandler error-handling routine:

Sub ErrFuncDemo()
   On Error GoTo ErrorHandler   ' Enable error-handling routine.
   Open "TESTFILE" For Output As #1   ' Open file for output.
   Kill "TESTFILE"   ' Attempt to delete open file.

   Dim d As Double

   d = 10 / sin(0)   ' "Division by zero" error 
   d = 20 / cos(0)

   Trace d

Exit Sub      ' Exit to avoid handler.

ErrorHandler:   ' Error-handling routine.
   errNumber = Err() ' Get error number

   Trace "ErrorNumber " & errNumber    

   Select Case errNumber   ' Evaluate error number.
      Case 55, 75   ' "File already open" or "Path/File access error" error.
         Trace """File already open"" or ""Path/File access error"" error"
         Close #1   ' Close open file.
      Case Else
         ' Handle other situations here...
         Resume Next
   End Select
   Resume   ' Resume execution at same line that caused the error.

End Sub

 

See Also

Erl Function , Error$ Function , On Error Statement , Trappable Errors