aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() method 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 "finalize() method" Watch "finalize() method" New topic
Author

finalize() method

Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
I'm a little confused.

I read that overriden finalize() method cannot throw any Checked exceptions.

However,

if i have the following finalize() method it compiles OK

public void finalize() throws IOException
{
//do some stuff
}

This contradicts with the fact...
can someone throw some light on this please...
thanks.

Saurabh
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Maybe you can define it, it may even compile.
But the exception will never be handled, it will in fact be silently eaten and ignored.

Very bad practice to attempt to throw anything from finalize().
In fact, you shouldn't really use finalize() at all as there's no guarantee it'll ever be executed (and even if, no guarantee as the when).


42
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Here's a bit of code that backs up what Jeroen said:


SCJA 1.0 (98%), SCJP 1.4 (98%)
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
Thanks for your help Jeroen and Joe!! That clarifies a few things....

vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
----------------------------------------------------------
Maybe you can define it, it may even compile.
But the exception will never be handled, it will in fact be silently eaten and ignored.

--------------------------------------------------------

no Jeroen Wenting UR very much wrong

how compiler will silently eat and ignore???
(is the food like by compiler ya)

its possible to throw exception in a overridden method in any derived class because Object class has a finalise() method with following syntax

protected void finalize() throws Throwable

check in java API
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
Originally posted by vidya sagar:
----------------------------------------------------------
Maybe you can define it, it may even compile.
But the exception will never be handled, it will in fact be silently eaten and ignored.

--------------------------------------------------------

no Jeroen Wenting UR very much wrong

how compiler will silently eat and ignore???
(is the food like by compiler ya)

its possible to throw exception in a overridden method in any derived class because Object class has a finalise() method with following syntax

protected void finalize() throws Throwable

check in java API


Hi Vidya Sagar,

According to Java specifications, the finalize() method can throw exception that is any sub class of Throwable (or Throwable itself). The garbage collector is what normally calls finalize() method (you won't normally be calling finalize() from your code explicitly), and the garbage collector does indeed ignore any exception thrown by the finalize() method and continues with its work.

Any comments folks??

regards
Saurabh
Saranyan Narayanan
Ranch Hand

Joined: Mar 12, 2003
Posts: 34
It is better to state that , compiler does look at allowing the exception to be handled by the runtime system, so in principle it does not prevent one from throwing exceptions in finalize
Originally posted by Saurabh Khanna:


Hi Vidya Sagar,

According to Java specifications, the finalize() method can throw exception that is any sub class of Throwable (or Throwable itself). The garbage collector is what normally calls finalize() method (you won't normally be calling finalize() from your code explicitly), and the garbage collector does indeed ignore any exception thrown by the finalize() method and continues with its work.

Any comments folks??

regards
Saurabh
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi Saranyan Narayanan

I accepting your argument
 
 
subject: finalize() method