Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Question about declaring finalize()

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Ruben and Vinodkumar. What you say makes sense; someone COULD subclass Throwable to make their own checked exception.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic