21.08.2013 Views

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Error Handling 38<br />

Sub Test<br />

On Error Resume Next<br />

' ... perform task during which an error may occur<br />

End Sub<br />

Use the On Error Resume Next command with caution as its effect is global.<br />

Queries Regarding Error Information<br />

In error handling, it is useful to have a description of the error and to know where and why<br />

the error occurred:<br />

• The Err variable contains the number of errors that has occurred.<br />

• The Error$ variable contains a description of the error.<br />

• The Erl variable contains the line number where the error occurred.<br />

The call<br />

MsgBox "Error " & Err & ": " & Error$ & " (line : "<br />

& Erl & ")"<br />

shows how the error information can be displayed in a message window.<br />

Whereas VBA summarizes the error messages in a statistical object called Err, <strong>OpenOffice</strong>.<strong>org</strong> Basic<br />

provides the Err, Error$, and Erl variables.<br />

The status information remains valid until the program encounters a Resume or On Error<br />

command, whereupon the information is reset.<br />

In VBA, the Err.Clear method of the Err object resets the error status after an error occurs. In<br />

<strong>OpenOffice</strong>.<strong>org</strong> Basic, this is accomplished with the On Error or Resume commands.<br />

Tips for Structured Error Handling<br />

Both the definition command, On Error, and the return command, Resume, are variants of<br />

the Goto construct.<br />

If you want to cleanly structure your code to prevent generating errors when you use this<br />

construct, you should not use jump commands without monitoring them.<br />

Care should be taken when you use the On Error Resume Next command as this dismisses<br />

all open error messages.<br />

The best solution is to use only one approach for error handling within a program - keep<br />

error handling separate from the actual program code and do not jump back to the original<br />

code after the error occurs.<br />

The following code is an example of an error handling procedure:<br />

Sub Example<br />

' Define error handler at the start of the function

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!