L
Lew
Robbert said:In particular, if you want the commonly useful behavior that any
exception should propagate up the call chain until it is handled, and if
it is not handled otherwise, the program should abort with an error
message, you still have to write "throws Exception" and a try ... catch
block (or, alternatively, convert checked exceptions to unchecked
exceptions at some point).
Not true. If you want to throw an exception that behaves as you describe,
simply throw an unchecked exception and handle it farther up the chain, just
as you say you want. No "mandatory throws clause" needed.
Of course, then you sacrifice the benefits of checked exceptions, but that is
your absolute right as a designer.
If someone else wrote a method with a checked exception that you want to use,
then that programmer is the one requiring you to handle the exception, not
Java, the language itself. That means that that programmer thought about their
API and decided that the exception was important enough to warrant mandatory
handling on the part of all the API's consumers.
Don't blame the language for the programmer's decision. They could have used
an unchecked exception but chose instead to declare a checked one. That is the
power of choice that Java gave them.
- Lew