Solved

Why do I need to handle or declare exception in the finalize method?

Posted on 2011-03-10
5
316 Views
Last Modified: 2012-05-11
I need help with Garbage Collection  in Java. I have 2 questions regarding finalize method:

Why do I need to handle or declare exception in the finalize method in line 12?
(When I replace super.finalize(), the code works fine)

I know finalize method should  execute at least once when the Garbage Collection starts working, so I compiled and run the code several times to see if I could find the finalize method prints “Hello” or “World” in the output (because I was hoping the Objects will be Garbage Collected). Why is it not producing any output after handling the exception? Let me know if I got the finalize and Garbage Collection concept right.

public class FinalizeTest {
    private String value;
    public FinalizeTest(String text) {
        this.value = text;
    }
    public static void main(String[] args) {
        FinalizeTest f1 = new FinalizeTest("Hello");
        FinalizeTest f2 = new FinalizeTest("World");
        f1 = null;
        f2 = null;
    }
    public void finalize(){ 
        System.out.println(this.value);
        super.finalize();
    }
} 

Open in new window

0
Comment
Question by:techbro
  • 3
5 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

This explains why it is a good practice to have a try/catch loop when overriding finalize:
http://www.janeg.ca/scjp/gc/finalize.html

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility


This works and you'll see the output:

public class FinalizeTest {
    private String value;
    public FinalizeTest(String text) {
        this.value = text;
    }
    public static void main(String[] args) {
        FinalizeTest f1 = new FinalizeTest("Hello");
        FinalizeTest f2 = new FinalizeTest("World");
        f1 = null;
        f2 = null;
        System.gc();
    }
    public void finalize() throws Throwable {
        try{
            System.out.println("ttt");
        System.out.println(this.value);
        }  catch(Exception ex){
            System.out.println("error " + ex.toString());
        }
        finally {
        }
        super.finalize();
        }  
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
This is the output that I see:

ttt
World
ttt
Hello

Open in new window


Otherwise (without System.gc()), perhpas we don't know when
GC would execute, especially if you are running it in IDE which, I guess, maintains
its own VM
 
0
 
LVL 10

Expert Comment

by:gordon_vt02
Comment Utility
Just to clarify, it is not guaranteed that your finalize method will ever be called by the JVM.  There are very rare instances where you actually need to use them and it is good practice to avoid them if at all possible.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
sumDigits  challenge 7 59
countAbc challenge 9 48
best (free) software to access postgres db (java) 1 17
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now