Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-10
5
Medium Priority
?
325 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
ID: 35099390

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 2000 total points
ID: 35099599


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
ID: 35099614
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
ID: 35109616
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

971 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