Contents | Prev | Next | Index


SYSTEM Exception Handling

Standard Oberon-2 language has no exception handling. The Java language, however, has some special statements for throwing, catching and handling exceptions. Exceptions are implemented as classes (records with type-bound procedures) which are extensions of the Java class java.lang.Throwable. All exception and error classes are extended from that class. Java uses the following class hierarchy for this:

Exceptions and errors are instances of above mentioned (possibly extended) classes. Instances of classes extended from java.lang.Error or java.lang.RuntimeException may be thrown at any time during program execution, usually resulting in a program abortion. They can, but must not, be caught and be dealt with by the program. Instances of java.lang.Exception and its extensions (except for java.lang.RuntimeException) are always to be caught and to be dealt with in the program. These are known in Java as checked exceptions.

This Oberon-2 compiler supports exception handling in two ways:

  1. If a checked exception is possibly thrown in a procedure body, e.g. as a result of calling a foreign Java method, this Oberon-2 compiler generates a default exception handler by wrapping the statement block in a Java try-catch-statement, as follows:

    try
    {
        statements
    }
    catch (java.lang.RuntimeException _e) { throw _e; }
    catch (java.lang.Exception _e) { SYSTEM.CANCEL( _e ); }

    That is, any exceptions other than those derived from java.lang.RuntimeException lead to a program abortion. An exception derived from java.lang.RuntimeException is being re-thrown in the default handler, hence it can be dealt with by any of the current method's callers. If no method deals with this kind of exception, the Java default action is to abort the program.

  2. An instance of an Exception or Error derived from the java.lang.Throwable class can be dealt with explicitly by using special generic SYSTEM procedures within the same statement sequence. They are described in the table below.   

Name Function
SYSTEM.THROW(Te) throw an error or exception of type Te. Te is the (possibly extended) Java type java.lang.Throwable.
SYSTEM.TRY start a exception-guarded statement sequence.
SYSTEM.CATCH(Te) catch an exception of type Te. Te is the (possibly extended) Java type java.lang.Throwable.
SYSTEM.FINALLY start of final statement sequence, always called at the end of SYSTEM.TRY statement.
SYSTEM.ENDTRY end of SYSTEM.TRY statement.

Explicit exception handling example:

MODULE MYSTUFF;
IMPORT
  SYSTEM,
  java_io_RandomAccessFile,
  java_io_IOException,
  java_io_EOFException;
 ...
TYPE
  File = java_io_RandomAccessFile.POINTER_TO_RandomAccessFile;
 ...
PROCEDURE ReadInteger( f : File ) : INTEGER;
VAR
  i : INTEGER;
BEGIN
  SYSTEM.TRY;
    i := f.readShort();
  SYSTEM.CATCH( java_io_EOFException.EOFException );
    (* end of file exception, use zero default *)
    i := 0;
  SYSTEM.CATCH( java_io_IOException.IOException );
    (* IOException occured, use zero default *)
    i := 0;
  SYSTEM.ENDTRY;
  RETURN i;
END ReadInteger;
 ...
END MYSTUFF.

 


Contents | Prev | Next | Index

Canterbury Oberon-2 for Java  (Last documentation update Feb 8, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com