Keywords: doc

Next: Java Interface Up: Exceptions Previous: Throwing Exceptions

## Catching Exceptions

Exceptions are always caught by reference using the `_var` types. System exceptions must be caught by `SystemException_var`:

      // ok
      try {
        ...
        mico_throw (CORBA::UNKNOWN());
        ...
      } catch (CORBA::SystemException_var &ex) {
        ...
      }

      // wrong
      try {
        ...
        mico_throw (CORBA::UNKNOWN());
        ...
      } catch (CORBA::UNKNOWN_var &ex) {
        ...
      }

      // wrong
      try {
        ...
        mico_throw (CORBA::UNKNOWN());
        ...
      } catch (CORBA::Exception_var &ex) {
        ...
      }

Sometimes it is necessary to know exactly which system exception has been thrown:

      // ok
      try {
        ...
        mico_throw (CORBA::UNKNOWN());
        ...
      } catch (CORBA::SystemException_var &sys_ex) {
        if (CORBA::UNKNOWN *ukn_ex = CORBA::UNKNOWN::_narrow (sys_ex)) {
          // something1
        } else {
          // something2
        }
      }

      // wrong
      try {
        ...
      } catch (CORBA::UNKNOWN_var &ukn_ex) {
        // something1
      } catch (CORBA::SystemException_var &other_ex) {
        // something2
      }

In contrast to system exceptions a user exception `X` must be caught by `X_var` (i.e., not by `UserException_var`):

      // ok
      try {
        ...
        mico_throw (SomeExcept());
        ...
      } catch (SomeExcept_var &some_ex) {
        ...
      }

      // wrong
      try {
        ...
        mico_throw (SomeExcept());
        ...
      } catch (CORBA::UserException_var &usr_ex) {
        ...
      }

      // wrong
      try {
        ...
        mico_throw (SomeExcept());
        ...
      } catch (CORBA::Exception_var &ex) {
        ...
      }

If an exception is thrown but not caught MICO will print out a short description of the exception and terminate the process. On systems where g++ does not support exception handling throwing an exception will always result in such a message and termination of the process.