Unlike UNO, Basic does not support exceptions. All exceptions thrown by UNO are caught by the Basic runtime system and transformed to a Basic error. Executing the following code results in a Basic error that interrupts the code execution and displays an error message:
Sub Main Dim oLib oLib = BasicLibraries.getByName( "InvalidLibraryName" ) End Sub
BasicLibraries object used in the example contains all the available Basic libraries in a running office instance. The Basic libraries contained in
BasicLibraries is accessed using com.sun.star.container.XNameAccess. An exception was provoked by trying to obtain a non-existing library. The
BasicLibraries object is explained in more detail in Advanced Library Organization.
The call to
getByName() results in this error box:
However, the Basic runtime system is not always able to recognize the Exception type. Sometimes only the exception message can be displayed that has to be provided by the object implementation.
Exceptions transformed to Basic errors can be handled just like any Basic error using the
On Error GoTo command:
Sub Main On Error Goto ErrorHandler ' Enables error handling Dim oLib oLib = BasicLibraries.getByName( "InvalidLibraryName" ) MsgBox "After the Error" Exit Sub ' Label ErrorHandler: MsgBox "Error code: " + Err + Chr$(13) + Error$ Resume Next ' Continues execution at the command following the error command End Sub
When the exception occurs, the execution continues at the
ErrorHandler label. In the error handler, some properties are used to get information about the error. The
Err is the error code that is 1 for UNO exceptions. The
Error$ contains the text of the error message. Executing the program results in the following output:
Another message box "After the Error" is displayed after the above dialog box, because
Resume Next goes to the code line below the line where the exception was thrown. The
Exit Sub command is required so that the error handler code would not be executed again.
|Content on this page is licensed under the Public Documentation License (PDL).|