Difference between revisions of "Documentation/DevGuide/ProUNO/Basic/Exception Handling"
m (1 revision(s)) |
OOoWikiBot (talk | contribs) m (Robot: Changing Category:Professional UNO) |
||
| Line 46: | Line 46: | ||
{{PDL1}} | {{PDL1}} | ||
| − | [[Category: Professional UNO]] | + | |
| + | [[Category:Documentation/Developers Guide/Professional UNO]] | ||
Revision as of 15:01, 8 May 2008
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
The 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 be executed again.
| Content on this page is licensed under the Public Documentation License (PDL). |

