Data Structures and Algorithms
Java Exceptions

Java, somewhat unfortunately, follows C++ and has the try, throw, catch mechanism.

However, Java defines a class hierarchy for errors, all of which are specialisations of the exception class.

Java exceptions, like Ada exceptions, are thrown from one class method to the invoking method, until it reaches a try block. However Java methods explicitly list the exceptions that they throw.

void readFromFile( String s ) throws IOException,
                     InterruptedException {

try {
    readFromFile( "abc" );
catch( FileNotFoundException e ) { ..... }
catch( IOException e) { .... }
catch( Exception e ) {
    // catches any other error (Exception is the "superclass")
finally {
    // Cleanup code - always executed
The finally is executed whether an exception is thrown or not.

Despite inheriting a mess, Java's designers have managed to simplify it to a useable system. catch appears (my text is not clear on this point - a problem with Java's instant fame!) to accept a single parameter belonging to the exception class. As in Ada, many exceptions are pre-defined, but you can also define your own.

Note the problem of a precise definition of Java is a perennial problem in the computer industry: the tendency to instantly adopt "fads" before they have a chance to be defined thoroughly and unambiguously. This has lead to enormous porting problems and enormous, avoidable costs for users.

No professional engineer would design any system using screws with non-standard threads!

Continue on to C ADT errors
Back to the Table of Contents
© John Morris, 1998