Solved

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

Posted on 2011-03-10
5
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

751 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