• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

finalize question

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what will happen if a checked exception is thrown from the code of finalize() method when the object is to be garbage collected?
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi mohamed

what will happen if a checked exception is thrown from the code of finalize() method ?when the object is to be garbage collected?


Let me use some coding to point out what happen if a checked exception is thrown from the code of finalize() method ?


public static void main(String[] argv)
{
try
{
finalize();//(c)
}
catch(IOException ioE)
{
System.out.println("file point to null");//(d)
}
finally
{
System.out.println("Finish");(e)
}
}
public static void finalize() throws IOException
{
File file=null;
FileWriter fw =new FileWriter(file); //(a)
System.out.prinln("Arrive forest"); //(b)
}


if a checked exception is thrown from the code of finalize() method,
firstly,(a) exception occur,it will jump out the method and create the IOException object,so (b) is not excecuted
then,the IOException object catch from the catch IOException handler,so print
file point to null (d)
And the IOException object will be collected after this catch handler,then print
Finish (e)
If it is not clear,please post again
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oo....
It may be easy to read

if a checked exception is thrown from the code of finalize() method,
firstly,(a) exception occur,create the IOException object and it will jump/throw out the method and terminate executing the remain part of programme
so (b) is not excecuted
then,the IOException object catch from the IOException handler,so print
file point to null (d)
And the IOException object will be collected after this catch handler,then print
Finish (e)
This time may be ok
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates. Object API, finalize()

When I first read that sentence from the API, I thought it meant the finalize method ignored the exception. But the Java Programming Language says

This method is declared to throw any exception but if an exception occurs it is ignored *by the garbage collector*.

To find out what happens in the finalize() method I ran this test.
 
reply
    Bookmark Topic Watch Topic
  • New Topic