Of course, Exception is "narrower" than Throwable, and I realize that super.finalize() throws a Throwable.
HOWEVER, it seems to me (although I'm not 100% sure) that everything that is a Throwable, and is NOT an Exception, is either an Error, or an Unchecked Exception. And my understanding is that Errors and unchecked exceptions (RuntimeException's) do NOT have to be "handled and declared".
(Even if I'm correct, I wouldn't think of this as a compiler bug... but I'm curious... I expected "throws Exception" to compile OK.)
At some point I was surprised by this too. It turns out that Throwable is a wider checked "exception" than Exception. I think the idea is that you could subclass Throwable directly, and have a checked "exception" hierarchy which wouldn't be a descendant of Exception, but as long as it is not a descendant of RuntimeException, it would be checked. This is just my interpretation of why this works this way though, and it might be incorrect.
The bottom line is that if you call a method which throws Throwable, you either have to catch Throwable, or declare that you throw a Throwable. It's not enough with catching or declaring Exception.
All code in my posts, unless a source is explicitly mentioned, is my own.
Throwable can be either checked or uncheked exception
and also same is the case with Exception it will be a checked exception unless it belongs to RuntimeException branch ,
so declaring it here like this
is basically violating the rule that : new or wider checked Exception that are not thrown by super class version of a method should not be thrown by its overriding versions
because when you are calling the super class version it might throw a Throwable which is super class to the class Exception (Since it's a super class version , it can throw a wider Exception)
so modifying the code to handle it will solve the issue:
or else if you modify the super class method implementation and make sure it throws Excepetion
it should work i guess.
for Throwable there is no problem because when you are saying
it should handle it or the method calling will handle it but its a Throwable in both cases which is the super class of all errors and exceptions in the Java language.
so its not showing any compilation error i guess.
I hope this explanation is correct.
Some one correct me if i am wrong
Regards
vinod SCJP 6.0
Leonard Fischer
Ranch Hand
Joined: Apr 17, 2009
Posts: 47
posted
0
Thanks to Ruben and Vinodkumar. What you say makes sense; someone COULD subclass Throwable to make their own checked exception.