Exception Handling in C++

From Apache OpenOffice Wiki
Jump to: navigation, search



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