D
Dan Wilkin
I have been having trouble understanding the motivation regarding the
inheritance hierarchy established with the top level exception classes
in java.lang. More specifically, I am wondering why java.lang.
RuntimeException (an unchecked exception) has been placed as a subclass
to java.lang.Exception (a checked exception)? An example:
public void A() {
try {
doWork();
doMoreWork();
doDiffWork();
catch (Exception e) {
System.out.error("An error occurred!");
e.printStackTrace();
processAnticipatedError(e);
}
}
In this sample try/catch block I would like to catch all propagated
checked exceptions that have potentially been thrown, yet declared by
the methods doWork(), doMoreWork(), doDiffWork() -- i.e. these methods
have declared they will throw checked exceptions. However, I do not
want to catch any runtime exceptions (RuntimeException and it's
subclasses) - I want those to propegate to my main() and crash my
program (or get handled gracefully there) so that I can know that I have
introduced a bad programming decision into my code that is failing the
contract of a lower level method (e.g., passing null somewhere thus
instigating an IllegalArgumentException, attempting to create a very
large object and running out of memory, etc.). At this point the code
above would catch *all* runtime exceptions as well as the checked
exceptions I was anticipating!!
Can someone give me a clear explaination why the Java authors created
the hierarchy in this way? Is there a JSR issue opened on this
particular item? Perhaps catching the general Exception class in this
case is poor programming practice and I should be writing catch blocks
for all possible checked exceptions (in which case they'd all be the
same - handled by my generic error handler - which in and of itself
stands for debate).
Nevertheless and in short, I would prefer to see Exception a subclass of
RuntimeException and checked/unchecked markings would hold to their
original classes within the JVM and Java language
specification/implementation (since this is also verified at compile
time). In other words, RuntimeException would still be an unchecked
exception and all its subclasses - except for Exception which would be
marked as checked, all subclasses of Exception would likewise be marked
as checked. RuntimeException would inherit from Throwable.
Dan
inheritance hierarchy established with the top level exception classes
in java.lang. More specifically, I am wondering why java.lang.
RuntimeException (an unchecked exception) has been placed as a subclass
to java.lang.Exception (a checked exception)? An example:
public void A() {
try {
doWork();
doMoreWork();
doDiffWork();
catch (Exception e) {
System.out.error("An error occurred!");
e.printStackTrace();
processAnticipatedError(e);
}
}
In this sample try/catch block I would like to catch all propagated
checked exceptions that have potentially been thrown, yet declared by
the methods doWork(), doMoreWork(), doDiffWork() -- i.e. these methods
have declared they will throw checked exceptions. However, I do not
want to catch any runtime exceptions (RuntimeException and it's
subclasses) - I want those to propegate to my main() and crash my
program (or get handled gracefully there) so that I can know that I have
introduced a bad programming decision into my code that is failing the
contract of a lower level method (e.g., passing null somewhere thus
instigating an IllegalArgumentException, attempting to create a very
large object and running out of memory, etc.). At this point the code
above would catch *all* runtime exceptions as well as the checked
exceptions I was anticipating!!
Can someone give me a clear explaination why the Java authors created
the hierarchy in this way? Is there a JSR issue opened on this
particular item? Perhaps catching the general Exception class in this
case is poor programming practice and I should be writing catch blocks
for all possible checked exceptions (in which case they'd all be the
same - handled by my generic error handler - which in and of itself
stands for debate).
Nevertheless and in short, I would prefer to see Exception a subclass of
RuntimeException and checked/unchecked markings would hold to their
original classes within the JVM and Java language
specification/implementation (since this is also verified at compile
time). In other words, RuntimeException would still be an unchecked
exception and all its subclasses - except for Exception which would be
marked as checked, all subclasses of Exception would likewise be marked
as checked. RuntimeException would inherit from Throwable.
Dan