TWCOS Kernel

Exceptions
Login

Exceptions

Introduction

Exception handling provides simpler (less error prone) and more logical (separate detection and handling) error handling than traditional return value based error handling used in most kernels.

Exceptions use KTRY/KCATCH/KFINALLY blocks of code, to delimit the various stages of exception management:

Example

The following example shows how exceptions are used:


/* Exception is a standard exception type */
EXCEPTION_DEF(ParentException,Exception);
EXCEPTION_DEF(MyException,ParentException);

...

void DoSomethingThatMightThrowAnException(...)
{
  ... Checks can throw exceptions to indicate invalid data ...
  check_arguments();

  ... Blah blah blah ...
  if (SomethingWentWrong) {
    KTHROW(MyException, "It all went horribly wrong");
  }
}

void Function()
{
  KTRY {

    ... Do stuff ...
    DoSomethingThatMightThrowAnException(...);
    ... This stuff won't be reached on exception above ...

  } KCATCH(MyException) {

    ... Oops - log an error ...
    ... Recover the situation if possible ...

  } KCATCH(Exception) {

    ... Oops - log more generic error ...

  } KFINALLY {

    ... Release any resources ...

  }
}