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

finalize question

mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
what will happen if a checked exception is thrown from the code of finalize() method when the object is to be garbage collected?
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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
SCJP, MCDBA
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize question