The error-related routines (see Table 10 ) in OOo Basic return information related to the last error. These routines are used to determine what happened and where. The error information is reset when the error handler is cleared, so save the error information if your macro program will require it later.
Function | Description |
---|---|
Erl | Line number of last error. |
Err | Error number of last error. |
Error Error(error_number) | Get error message either for the last error or for the specified error message. |
The macro in Listing 17 checks for an error before the error handler is reset. Error information is then saved. Although the macro in Listing 17 does not save the error message, the Error function accepts an error number as an optional argument for which the error message is returned. Also see Figure 6 .
Sub ExampleError On Error Goto BadError 'Set up the error handler Print 1/ CInt(0.2) 'Do a division by zero BadError: 'Error handler starts here Dim s As String 'Accumulate the message Dim oldError As Integer 'Save the error number Dim lineNum As Integer 'Save the line number If Err <> 0 Then 'If an error occurred oldError = Err 'Save the error number lineNum = Erl 'Save the error line number s = "Before clearing the error handler" & CHR$(10) &_ "Error number " & Err & " Occurred at line number " & Erl & CHR$(10) &_ "Err message: " & Error() & CHR$(10) End If On Error Goto 0 'Reset the error handler REM There is now no information to print s = s & CHR$(10) & "After clearing handler:" & CHR$(10) &_ "Error number " & Err & " Occurred at line number " & Erl & CHR$(10) REM Use the saved information s = s & CHR$(10) & "Error info was saved so:" & CHR$(10) &_ "Error number " & oldError & " Occurred at line number " &_ lineNum & CHR$(10) & "Err message: " & Error(oldError) MsgBox s, 0, "Error Handling" End Sub