Difference between revisions of "Documentation/DevGuide/ProUNO/C++/Exception Handling in C++"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
 
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>-->
 
For throwing and catching of UNO exceptions, use the normal C++ exception handling mechanisms. Calls to UNO interfaces may only throw the <code>com::sun::star::uno::Exception</code> or derived exceptions. The following example catches every possible exception:
 
For throwing and catching of UNO exceptions, use the normal C++ exception handling mechanisms. Calls to UNO interfaces may only throw the <code>com::sun::star::uno::Exception</code> or derived exceptions. The following example catches every possible exception:
 
+
<syntaxhighlight lang="cpp">
 
   try  
 
   try  
 
   {
 
   {
Line 22: Line 22:
 
       printf( "An error occurred: %s\n", o.pData->buffer );
 
       printf( "An error occurred: %s\n", o.pData->buffer );
 
   }
 
   }
 
+
</syntaxhighlight>
 
If you want to react differently for each possible exception type, look up the exceptions that may be thrown by a certain method. For instance the <code>resolve()</code> method in <idl>com.sun.star.bridge.XUnoUrlResolver</idl> is allowed to throw three kinds of exceptions. Catch each exception type separately:
 
If you want to react differently for each possible exception type, look up the exceptions that may be thrown by a certain method. For instance the <code>resolve()</code> method in <idl>com.sun.star.bridge.XUnoUrlResolver</idl> is allowed to throw three kinds of exceptions. Catch each exception type separately:
 
+
<syntaxhighlight lang="cpp">
 
   try
 
   try
 
   {
 
   {
Line 55: Line 55:
 
       printf( "an unknown error has occurred\n" );
 
       printf( "an unknown error has occurred\n" );
 
   }
 
   }
 
+
</syntaxhighlight>
 
When implementing your own UNO objects (see [[Documentation/DevGuide/WritingUNO/C++/C++ Component|C++ Component]]), throw exceptions using the normal C++ throw statement:
 
When implementing your own UNO objects (see [[Documentation/DevGuide/WritingUNO/C++/C++ Component|C++ Component]]), throw exceptions using the normal C++ throw statement:
 
+
<syntaxhighlight lang="cpp">
 
   void MyUnoObject::initialize( const Sequence< Any > & args.getLength() ) throw( Exception )
 
   void MyUnoObject::initialize( const Sequence< Any > & args.getLength() ) throw( Exception )
 
   {
 
   {
Line 74: Line 74:
 
       ...
 
       ...
 
   }
 
   }
 
+
</syntaxhighlight>
 
Note that only exceptions derived from <code>com::sun::star::uno::Exception</code> may be thrown at UNO interface methods. Other exceptions (for instance the C++ std::exception) cannot be bridged by the UNO runtime if the caller and called object are not within the same UNO Runtime Environment. Moreover, most current Unix C++ compilers, for instance gcc 3.0.x, do not compile code. During compilation, exception specifications are loosen in derived classes by throwing exceptions other than the exceptions specified in the interface that it is derived. Throwing unspecified exceptions leads to a std::unexpected exception and causes the program to abort on Unix systems.
 
Note that only exceptions derived from <code>com::sun::star::uno::Exception</code> may be thrown at UNO interface methods. Other exceptions (for instance the C++ std::exception) cannot be bridged by the UNO runtime if the caller and called object are not within the same UNO Runtime Environment. Moreover, most current Unix C++ compilers, for instance gcc 3.0.x, do not compile code. During compilation, exception specifications are loosen in derived classes by throwing exceptions other than the exceptions specified in the interface that it is derived. Throwing unspecified exceptions leads to a std::unexpected exception and causes the program to abort on Unix systems.
  

Latest revision as of 13:16, 23 December 2020



For throwing and catching of UNO exceptions, use the normal C++ exception handling mechanisms. Calls to UNO interfaces may only throw the com::sun::star::uno::Exception or derived exceptions. The following example catches every possible exception:

  try 
  {
      Reference< XInterface > rInitialObject = 
      xUnoUrlResolver->resolve( OUString::createFromAsci( 
      "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" ) );
  }
      catch( com::sun::star::uno::Exception &e )
  {
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      printf( "An error occurred: %s\n", o.pData->buffer );
  }

If you want to react differently for each possible exception type, look up the exceptions that may be thrown by a certain method. For instance the resolve() method in com.sun.star.bridge.XUnoUrlResolver is allowed to throw three kinds of exceptions. Catch each exception type separately:

  try
  {
      Reference< XInterface > rInitialObject = 
      xUnoUrlResolver->resolve( OUString::createFromAsci( 
      "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" ) );
  }
  catch( ConnectionSetupException &e )
  {
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      printf( "%s\n", o.pData->buffer );
      printf( "couldn't access local resource ( possible security resons )\n" );
  }
  catch( NoConnectException &e )
  {
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      printf( "%s\n", o.pData->buffer );
      printf( "no server listening on the resource\n" );
  }
  catch( IllegalArgumentException &e )
  {
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      printf( "%s\n", o.pData->buffer );
      printf( "uno URL invalid\n" );
  }
  catch( RuntimeException & e )
  {
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      printf( "%s\n", o.pData->buffer );
      printf( "an unknown error has occurred\n" );
  }

When implementing your own UNO objects (see C++ Component), throw exceptions using the normal C++ throw statement:

  void MyUnoObject::initialize( const Sequence< Any > & args.getLength() ) throw( Exception )
  {
      // we expect 2 elements in this sequence
      if( 2 != args.getLength() )
      {
          // create an error message
          OUStringBuffer buf;
          buf.appendAscii( "MyUnoObject::initialize, expected 2 args, got " );
          buf.append( args.getLength() );
          buf.append( "." );
 
          // throw the exception
          throw Exception( buf.makeStringAndClear() , *this );
      }
      ...
  }

Note that only exceptions derived from com::sun::star::uno::Exception may be thrown at UNO interface methods. Other exceptions (for instance the C++ std::exception) cannot be bridged by the UNO runtime if the caller and called object are not within the same UNO Runtime Environment. Moreover, most current Unix C++ compilers, for instance gcc 3.0.x, do not compile code. During compilation, exception specifications are loosen in derived classes by throwing exceptions other than the exceptions specified in the interface that it is derived. Throwing unspecified exceptions leads to a std::unexpected exception and causes the program to abort on Unix systems.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages