Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize() method

 
Saurabh Khanna
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a bit of code that backs up what Jeroen said:
 
Saurabh Khanna
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help Jeroen and Joe!! That clarifies a few things....

 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----------------------------------------------------------
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saranyan Narayanan

I accepting your argument
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic