Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-10
5
Medium Priority
?
324 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 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

[Webinar] Protection from Cyberattacks

In this session, we’ll dive into the complexities of modern cyber threats and why only multi-vector protection can keep today’s businesses secure through the various stages of a cyberattack, across multiple vectors. Thursday September 14, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

730 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