Last week, I had my first hands-on Java experience. I had
previously read a few of the Java related books to come out of
Sun, so I know quite a lot about the language. But, as always,
actually doing something can provide instant insights you didn't
previously have. This one was related to exceptions.
I had always thought the Java thing of declaring which exceptions a method can throw to be a neat documentation trick, but how wrong was I!
I wanted to display a little graphic in an Applet and update it, say,
every second. The Thread class delivers a sleep method so that
seemed the method to be called. I had (still have) the impression
that the sleep-and-redraw loop had to be done in a separate
thread, so I made my class implement the Runnable interface, and
wrote my little run method.
The new run method's heading becomes
So, I am forced to adhere to the Runnable interface because I want to deliver a method (which must be the run method) that a new Thread can invoke. Because the interface dictates nothing about exceptions, my run method must catch any exception that any of the methods it invokes may raise.
What is it that my run method must do with the exception? It is not
allowed to raise anything, so the best thing it can do is output
some information regarding the situation, but followed by what?
Should it continue execution as if nothing happened? That is a
bad idea; the exception was not raised for nothing. Should it
stop execution and return? In that case the calling method has no
way of telling whether the method returned because it should or
because it was forced to. And if my method did not return
Concluding: having to declare exceptions with a method forces one into local error handling at inappropriate places. This is inferior when compared to total freedom, and contrary to what exceptions are meant for.
Some notes (Mon Mar 2 1998)
Apparantly, RuntimeException exceptions do not need to be declared (thank you Michael for pointing this out). Which only shows that the exception declaration concept is broken: `You must declare the exceptions that can be raised, except the ones that would need to be included in every method.' To paraphrase a famous quote: All exceptions are equal, except...
As an indication of the mis-use of exceptions given the restriction
discussed in this highlight, let me quote a randomly chosen piece
of code from the Code
Examples for The Java Class Libraries: Second Edition, Volume
2 (from java/awt/Button/Main.java, reformatted):
More notes (Mon Sep 28 1998)
After actually reading what has been written on the subject of `exception specifications', I should add to the above remark that Java discerns checked and unchecked exceptions. The latter include RuntimeException and Error; the former include the rest. The point made in this highlight is not affected by this clarification.