• 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

Garbage Collection

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize() method will be called only once when an object is garbage collected. What happens if the finalize() method itself throws an Exception.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can catch it by try/catch() block

if not catch then i think demon thread have to printStackTrace for that error...

Am i right???
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if the finalize() method itself throws an Exception.

Exception is ignored and finalization for that object is terminated.For that object finalize is not called any more.

if not catch then i think demon thread have to printStackTrace for that error...

Hope it wont happen so.But not sure.
 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;


public class finalize{

public static void main(String args[]){

try{

File file = new File("main_output.txt");
FileWriter f = new FileWriter(file);

for(int i=0;i<1000000;i++){
f fobj = new f();
fobj = null;
}

f.write("Main Sucessfully");
f.flush();
f.close();

}catch(Exception e){
e.printStackTrace();
}


}


public void finalize(){

try{
PrintWriter pr = new PrintWriter(new FileOutputStream("finalize_output.txt",true));

try{

int k=10;

int i = k/0;

}catch(Exception e){
e.printStackTrace(pr);
}

pr.write("Finalize Sucessfully");
pr.flush();
pr.close();

}catch(Exception e)
{
e.printStackTrace();
}

}

}


I have test above program

Got Result:


main_output.txt:

Main Sucessfully


finalize_output.txt:

java.lang.ArithmeticException: / by zero
at f.finalize(f.java:44)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Unknown Source)
at java.lang.ref.Finalizer.access$100(Unknown Source)
at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
Finalize Sucessfully

(Many Many times....)

which Thraed generate this error main ot another one(garbaze Collector Thraed) ???
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread.


if the finalize method throws an exception the finalization will be interrupted, but the release of the memory of the object will not stop because of this.

 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic