• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Can overridden finalize() method declare checked exceptions?

 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here is my class file which runs well and good.

package gc;
class UException extends Exception{
}
class POJO1
{
POJO1(){
System.out.println("Creating up POJO1");

}
protected void finalize() throws Throwable{
System.out.println("Destroying up POJO1");
super.finalize();
}
}
class POJO2 extends POJO1
{
POJO2(){
System.out.println("Creating up POJO2");

}
protected void finalize() throws UException{
System.out.println("Destroying up POJO1");
try{
super.finalize();
}catch(Throwable e){
throw new UException();
}
}
}
class TestGC{
public static void main(String[] args)
{
for(int i=0;i<5;i++){
new POJO2();
}
System.out.println("Bye");

}
}

As I read the Khalid Moghul(in page 535,8.4d), it says "Overriding finalize() methods can limit the range of throwables to unchecked exceptions. Further overridden definitions of this method in subclasses will not be able to throw checked exceptions".

But as above code proves that we can declare checked exceptions inthe overridden methods of finalize(). If somebody knows, please clarify me.

Thanks and Regards,
Narendranath
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

protected void finalize() throws Throwable{

protected void finalize() throws UException{



overiding method must have either Throwable or its subclass


here Throwable is superclass of all exceptions
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution is not appropriate to my question. Please somebody clarify me.

Regards,
Narendranath
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement from the book is correct. There are two things in it.

1) Note that the author says "CAN" and not "MUST":

Overriding finalize methods can limit the range of throwables to unchecked exceptions. Further overridden definitions of this method in subclasses will not be able to throw checked exceptions






Notice here that NumberFormatException is an un-checked exception
(i.e. some subclass of RuntimeException). Now in class A we have
done exactly this: we have limited the range of the Throwables
to unchecked exceptions only ( the signature of finalize() in
Object was : "protected void finalize() throws Throwable" )

2) The second thing which the author means is that if you do
that in A then in all classes which derive from A you will not
be able to declare something like:


This is quite clear because B can not override any method from
its superclass and make it throw "more" checked exceptions than
the superclass. And as finalize in A throws no checked exceptions
at all class which directly or indirectly derive from A can not
throw any checked exceptions as well.

So the answer to the question is:
finalize() can throw checked exceptions but only
of classes which are either the same classes or are subclasses of
the checked exceptions which super.finalize() has declared to throw.

Hope this makes things clearer.

[ February 13, 2006: Message edited by: Peter Petrov ]

[ February 13, 2006: Message edited by: Peter Petrov ]

[ February 13, 2006: Message edited by: Peter Petrov ]

[ February 13, 2006: Message edited by: Peter Petrov ]
[ February 13, 2006: Message edited by: Peter Petrov ]
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

the answer is quite clear.
For a overriden method, about the exceptions thrown, the rule is :
Checked excpetions :
1 - you can throw the same exceptions thrown by the parent class' method, a subclass of these, or none;
2 - you cannot throw any other checked exception than those allowed by first rule;
3 - you can always add unchecked exceptions not declared by parent's method.

Unchecked exceptions :
4 - you can throw any unchecked exception, no consideration is made about parent's method list of thrown unchecked exception;
5 - if the parent's method throws only unchecked exception, rule 1 doesn't allow you to throw any checked exception, as they are not subclasses of parent's method thrown exceptions (or no exception at all).

So the saying from the book refers to rule 5 here, as declaring an unchecked exception in parent's finalize method limits the child's method to unchecked exceptions;

Your code compiles because in first method, you declare Throwable, parent of all excpetions, including checked ones, so it's allowed to declare in second method a checked exception, because it's definitely a subclass of Throwable.

A last word about finalize method : the exceptions thrown in this method are ignored by the GC.

Hope this helps,
VK
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for clarifying me.

Regards,
Narendranath
 
Tell me how it all turns out. Here is a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic