Contents | Prev | Next | Index
Standard Modula-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 (possxibly 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 Modula-2 compiler supports exception handling in two ways:
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 Modula-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