Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of

The chase.

We have an array that contains 3 Strings, so only 0, 1 and 2 are valid indexes .

When the button is clicked or Enter is typed in the TextField, the text from the TextField is retrieved as a String.

The java.lang.Integer class's parseInt method tries to make an int from this text.

If this is not possible, the parseInt method generates ('throws') a NumberFormatException, causing the program to jump to the corresponding catch block.

Then the program skips the next catch block and jumps to the finally block.

Then the last statements of the actionPerformed method are executed, because the exception has been caught, has been handled.

When a number has been entered in the TextField, that can be succesfully converted to an int, this is used as an index into the array.

If inx is not a valid index, the program skips the first catch block (wrong type) and jumps to the proper catch block.

After execution, the statements in the finally block are executed and the remaining statements in actionPerformed.

If all goes well, the program reaches this line,....

, skips the catch blocks, but executes the statements in the finally block anyway, and of course the remaining statements.

If an exception occurs that is not caught here, the only statements that are executed, are those in the finally block.

So the finally block is a perfect place to perform clean-up, close connections, handle things properly, because it is executed whatever happens.


Actually, an ArrayIndexOutOfBoundsException occurs, which extends IndexOutOfBoundsException.

According to this principle, you could handle exceptions like FileNotFoundEx. and EOFEx. in an IOException catch.


This is WRONG,.....

because the EOFException is NEVER reached. The catches should be swapped.


Exceptions that derive from Exception, but NOT from RuntimeException, have to be handled. These are CHECKED exceptions, checked by the compiler.

You can find out what exceptions a method can throw in the documentation. If you lookup Integer.parseInt, you'll find that it THROWS NumberFormatException.




Notice the syntax : curly brackets.