File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about declaring finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Question about declaring finalize()" Watch "Question about declaring finalize()" New topic
Author

Question about declaring finalize()

Leonard Fischer
Ranch Hand

Joined: Apr 17, 2009
Posts: 47
Hello everyone. I can declare a finalize() method as follows:

protected void finalize() throws Throwable {
out.println("in finalize()");
super.finalize();
}

No problem. But I don't understand why the following does not compile:

protected void finalize() throws Exception{
out.println("in finalize()");
super.finalize();
}

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.)

Thanks!

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.
Vinod Kumar Kommineni
Ranch Hand

Joined: Jun 12, 2008
Posts: 54

Hi Leonard

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
Thanks to Ruben and Vinodkumar. What you say makes sense; someone COULD subclass Throwable to make their own checked exception.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Question about declaring finalize()
 
Similar Threads
Usage of Throwable
Destructor
finalize method
Can overridden finalize() method declare checked exceptions?
If a Runtime Exception is thrown in the finalize method -