Difference between revisions of "Documentation/DevGuide/ProUNO/Basic/Exception Handling"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
m
 
Line 10: Line 10:
 
<!--<idltopic>com.sun.star.uno.Exception;com.sun.star.uno.RuntimeException</idltopic>-->
 
<!--<idltopic>com.sun.star.uno.Exception;com.sun.star.uno.RuntimeException</idltopic>-->
 
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:
 
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:
 
+
<source lang="oobas">
 
   Sub Main
 
   Sub Main
 
       Dim oLib
 
       Dim oLib
 
       oLib = BasicLibraries.getByName( "InvalidLibraryName" )
 
       oLib = BasicLibraries.getByName( "InvalidLibraryName" )
 
   End Sub
 
   End Sub
 
+
</source>
 
The <code>BasicLibraries</code> object used in the example contains all the available Basic libraries in a running office instance. The Basic libraries contained in <code>BasicLibraries</code> is accessed using <idl>com.sun.star.container.XNameAccess</idl>. An exception was provoked by trying to obtain a non-existing library. The <code>BasicLibraries</code> object is explained in more detail in [[Documentation/DevGuide/Basic/Advanced Library Organization|Advanced Library Organization]].
 
The <code>BasicLibraries</code> object used in the example contains all the available Basic libraries in a running office instance. The Basic libraries contained in <code>BasicLibraries</code> is accessed using <idl>com.sun.star.container.XNameAccess</idl>. An exception was provoked by trying to obtain a non-existing library. The <code>BasicLibraries</code> object is explained in more detail in [[Documentation/DevGuide/Basic/Advanced Library Organization|Advanced Library Organization]].
  
Line 25: Line 25:
  
 
Exceptions transformed to Basic errors can be handled just like any Basic error using the <code>On Error GoTo</code> command:
 
Exceptions transformed to Basic errors can be handled just like any Basic error using the <code>On Error GoTo</code> command:
 
+
<source lang="oobas">
 
   Sub Main
 
   Sub Main
 
       On Error Goto ErrorHandler ' Enables error handling
 
       On Error Goto ErrorHandler ' Enables error handling
Line 39: Line 39:
 
       Resume Next ' Continues execution at the command following the error command
 
       Resume Next ' Continues execution at the command following the error command
 
   End Sub
 
   End Sub
 
+
</source>
 
When the exception occurs, the execution continues at the <code>ErrorHandler</code> label. In the error handler, some properties are used to get information about the error. The <code>Err</code> is the error code that is 1 for UNO exceptions. The <code>Error$</code> contains the text of the error message. Executing the program results in the following output:
 
When the exception occurs, the execution continues at the <code>ErrorHandler</code> label. In the error handler, some properties are used to get information about the error. The <code>Err</code> is the error code that is 1 for UNO exceptions. The <code>Error$</code> contains the text of the error message. Executing the program results in the following output:
  

Latest revision as of 11:41, 22 October 2009



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:

Unhandled UNO Exception

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:

Handled UNO Exception

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).
Personal tools
In other languages