On Wed, 7 Feb 2007, James Harris wrote:
>
> I have a number of books on Java but none seem to answer the
> fundamental question on throws clauses: /why/ force the programmer to
> declare what a method /may/ throw?
>
> To quote one: "If you write a method that throws an exception, then
> the Java compiler wil require that you do one of two things: you must
> either declare that the method throws the exception using the 'throws'
> keyword, or else you must provide a catch exception handler to catch
> that exception." - Java Programming Explorer.
>
> Is there really value in Java's behaviour? Or is this an unnecessary
> burden on the programmer? Why not simply accept exceptions can be
> thrown which are not listed, and deal with them in the innermost catch
> clause which matches?
You probably know that C++ treats exceptions that way, although I
think they just did it that way so that you could mix old exceptionless
(unexceptional?) code with new exception-throwing code.
Also, if you make exception specifications part of the function's
"prototype", it would be one more thing to annoy programmers who use
function pointers. It's bad enough that I can't pass 'strcmp' as the
fourth argument to 'qsort'; now you're telling me I can't even pass
'int blah(const void *, const void *) throws(Bletch)' because its
exception specification doesn't match what 'qsort' expects?
But those considerations are all due to C++'s legacy from C, and I
don't think they apply to Java except insofar as a lot of Java programmers
came over from C and C++ in the early days. Exception handling is still
arcane enough that "because C++ does it that way" isn't a winning
argument, the way it is with operator precedence or control structures.
So, what are the benefits of explicitly declaring exception
specifications? First, psychological reasons; I recommend that
everyone read Tom Cargill's "Exception Handling: A false sense of
security", from 1994, which demonstrates how easy it is to forget
what you're doing when you write code involving exceptions.
http://www.awprofessional.com/conten...g_Article.html
Clearly, having to write exception specifications for all his member
functions would not have saved Reed; but might it have made him
think a little bit harder, and maybe eliminated one or two bugs
out of a dozen?
Second, there might be technical reasons to prefer that functions
declare anything they expect to catch, throw, or pass through.
Obviously we don't /need/ that information, because if we did, it would
be impossible to compile C++!

But it might help. See this snippet
of "exception-handling" code in C, which needs the concept of a "rethrow"
clause to be implementable in ISO standard C:
http://en.wikipedia.org/wiki/Talk:Se...d_catch_blocks
So if you're translating Java to ISO C, it might be useful.
[...]
> In terms of code maintenance, if dd could once throw a given exception
> but now, because the relevant code has been removed, it cannot does cc
> still need to provide for handling the exception simply /because/ it
> is listed in dd's throws clause?
In terms of code maintenance, heck yes! If the programmer has removed
DD's ability to throw E, he should also have changed DD's exception
specification. If the compiler can warn him about his oversight, that's
a good thing in my book.
-Arthur