This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

finalize() method

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Jeroen and Joe!! That clarifies a few things....

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

I accepting your argument
 
Do not threaten THIS beaver! Not even with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic