company logo

Exception Handling

Keywords:  exceptionexception class

OSI supports exception handling, which allows reacting on unexpected events (exceptions). Exception handling includes setting (firing) exceptions and handling exceptions.

Exceptions can be handled in an error block. Exceptions that are not handled are passed to the calling method. Exceptions can be suppressed, when not being handled.

Generating an exception in an OSI method

Exceptions are generated depending on the exception class by the operating system (program exceptions), by the database system (data exceptions), by the ODABA Script Interface (language exceptions) or by the application (user-defined exceptions). The application may generate an exception using the EXCEPTION() function. When an exception has been generated, it is passed to the calling method until it is handles and reset.

For generating an application exception you may call the EXCEPTION() function passing an exception name. The exception name can be used to identify the exception when being handled.

exception("Car_without_wheels");

You may also pass a number of parameters to the exception, which can be used for evaluating the exception.

exception("Car_without_wheels",car_id,owner);

When an exception is thrown, a message with the following structure is written to the error log file:

except_type exception except_name raised in 'class_name::expr_name" line nnn

Parameters passed to the exception are evaluated internally, only, but not passed to the error message.Exception parameters can be evaluated when handling the exception. Exception parameters are passed by value.

Handle OSI exceptions

When an OSI exception is raised, the control is passed to the next ON_ERROR block. When no ON_ERROR block has been defined in the method, the control is passed to the first ON_ERROR block in the hierarchy of calling methods. When no ON_ERROR block is found, the application terminates.

When an exception has raised, all exceptions are suppressed until the exception is reset. You may reset the exception in the ON_Error block using the RestException() function.

Within the ON_ERROR block you may check all exceptions or exceptions of a given exception class. The ExceptionClass() function returns the exception class, the exception raised belongs to. The ExceptionName() function returns the identifier for the exception, which might be a number or name depending on the exception class.

{

process

  while ( Next() )

      printf("The name of person %s is: %s\n", (string)pid, name);

on_error

  switch ( ExceptionClass() )

  {

    case 'DataException' :

      switch ( Exception('DataException') )

      {

        case 3 : Message("Accessing deleted person");

          break;

        case 6 : Message("Instance locked for read");

          break;

        default : Message("Unknown database error");

      }

      ResetException();

      resume;

    default : break;

  }

}