Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9184639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:06:13+00:00 2026-06-17T19:06:13+00:00

Are there any details on whether or not an object is cleaned up using

  • 0

Are there any details on whether or not an object is cleaned up using finalize() if that object’s constructor thew an exception.

When this method is called is notoriously ill defined. According to the manual:

The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is guaranteed,
however, that the thread that invokes finalize will not be holding any
user-visible synchronization locks when finalize is invoked. If an
uncaught exception is thrown by the finalize method, the exception is
ignored and finalization of that object terminates.

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize%28%29

I’ve not been able to trigger the finalize method in this way. Does anyone know if it is garunteed NOT to be called or if it is in some cases called after the constructor failed to initialize the object (thew an exception).

I ask this because I have an object which must not be cleaned up twice. I’m trying to understand if it is safe to clean up before throwing the exception or if I must leave a marker for finalize() to effectively skip and do nothing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:06:14+00:00Added an answer on June 17, 2026 at 7:06 pm

    My test shows that it can

    public class Test1 {
    
        Test1() {
            throw new RuntimeException();
        }
    
        @Override
        protected void finalize() throws Throwable {
            System.out.println("finalized");
        }
    
        public static void main(String[] args) throws Exception {
            try {
                new Test1();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
            System.gc();
            Thread.sleep(1000);
        }
    }
    

    prints

    java.lang.RuntimeException
        at test.Test1.<init>(Test1.java:13)
        at test.Test1.main(Test1.java:24)
    finalized
    

    it is on Java HostSpot Client VM 1.7.0_03

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any good, free tools to profile memory usage in C# ? Details:
Is there any way I can set a formatter on models that will convert
Is there any way we can fetch X509 Public Cetrificates using c# from AD
I am using ApacheDS version 2.0.0-M3 (I not very familiar with how this works).
Is there any reason to consider XHTML related details for web development for someone
I'm working with some imported data that stores details about whether a room is
Is there any way I can get VS2008 to provide further detail on how
Is there any functional difference in Python between a try statement and an if
Is there any way in Notepad++ (or even with another tool) to change the
Are there any recommended libraries, patterns or examples on how to implement Prefetching and/or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.