aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can overridden finalize() method declare checked exceptions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can overridden finalize() method declare checked exceptions?" Watch "Can overridden finalize() method declare checked exceptions?" New topic
Author

Can overridden finalize() method declare checked exceptions?

Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

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


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
srilatha kareddy
Ranch Hand

Joined: Jan 12, 2006
Posts: 32
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

Joined: Feb 03, 2004
Posts: 577

Your solution is not appropriate to my question. Please somebody clarify me.

Regards,
Narendranath
Peter Petrov
Greenhorn

Joined: Feb 03, 2006
Posts: 17
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 ]
Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
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


SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources: http://esnault.frederic.free.fr/certification
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Thanks guys for clarifying me.

Regards,
Narendranath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can overridden finalize() method declare checked exceptions?