Exception Types


| Basic Types | Enum Types | Object Types | Array Types | Interface Types | Exception Types | Member Types | Method Types | Namespace Types | Foreign Types |

Exception types used to signal errors or other exeptional conditions.

Content of this chapter:

   Type system of exceptions
     Throwable as base of all exceptions
     Declaring & defining Exceptions
   Usage of exceptions
     Throwing an exceptions
     Catching exception

 Type system of exceptions

The type system of ACDK follows the Java modell.

 Throwable as base of all exceptions

The base class of all exeptions is the class  acdk::lang::Throwable.
It the same interface and functionallity, like the Java class java.lang.Throwable.

 Declaring & defining Exceptions

ACDK Exceptions are a little bit different to ordinary ACDK classes:

// ...
// declare the MyException, derived from Exception
// defines also RMyException
ACDK_DECL_THROWABLE(MyException, Exception);

// alternativelly fully qualified:
ACDK_DECL_THROWABLE_FQ(MyException, acdk::lang::, Exception);

class MyException
: extends acdk::lang::Exception
  ACDK_WITH_METAINFO(MyException) // optional, for class information see  Metainfo
  MyException() : Exception() {}
  MyException(RString what) : Exception(what) {}

It is import to use the ACDK_DECL_THROWABLE or ACDK_DECL_THROWABLE_FQ macros to declare the exception, becuase this enables structured - based on the class derivation - exception handling.

 Usage of exceptions

 Throwing an exceptions

To throw an exception the best way is to use one the THROW macros:

  // throw exception with 0 arguments
  // throw exception with 1 argument
  THROW1(Error, "An general Error"); 
  // throw fully qualified (namespace) exception with 1 argument 
  THROW1_FQ(acdk::io::, IOException, "File not open"); 

Exception are thrown and catched as reference. This is the reason why following constructs are not working the ACDK way:

  // NOT OK, because throws 'Error'
  throw Error("An general Error");
  // NOT OK, because throws 'Error*'
  throw new Error("An general Error");
  // OK, because throws 'RError'
  THROW1(Error, "An general Error");
  // OK, because throws 'RError'
  // but not recommended, because may 
  // not be handled by Script debugger
  throw RError(new Error("An general Error"));

 Catching exception

Exception can be catched following way:

void foo()
  try {
  } catch (acdk::io::RIOException ex) {
    // handle here type of IOException.
  } catch (RThrowable ex) {
    // handle all other ACDK exceptions
     otherwise Nullpointer exception will not handled properly
  } catch (...) {
    // this are not ACDK exception 
    // for example std::exception, if you use STL

See also:  Exception declaration.

See also:  Throwable.