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

  • Home
  • SEARCH
  • 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 8610953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:15:03+00:00 2026-06-12T04:15:03+00:00

Firstly, sorry for asking a question on what must be a well-worn topic. I’ve

  • 0

Firstly, sorry for asking a question on what must be a well-worn topic. I’ve come across loads of questions with answers suggesting to swallow (catch and ignore/log) potential exception from methods that close resources in finally blocks. It seems to be a generally accepted pattern. But I haven’t yet seen anybody explain why. Here’s just one example: Try-catch-finally and then again a try catch.

I understand that any exception thrown from a finally block will “mask” any exception thrown in the corresponding try block, but I don’t see why this is a bad thing. For example:

Resource r = new Resource();
try {
    r.use();
    other();
} finally {
    r.close();
}

My current understanding is:

  1. If only close throws an exception, we definitely don’t want to swallow it.
  2. If both use and close throw an exception, it’s probably for the same underlying reason, and it doesn’t matter which exception gets propagated up (they both would contain equally useful information?).
  3. If both other and close throw an exception, there are two unrelated problems. Arguably the one that happened first should propagate, but there isn’t any reason to suggest that the first exception caused the second (is there?), so either will do.

What am I wrong about?

  • 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-12T04:15:05+00:00Added an answer on June 12, 2026 at 4:15 am

    If only close throws an exception, we definitely don’t want to swallow it.

    That’s right!

    If both use and close throw an exception, it’s probably for the same underlying reason, and it doesn’t matter which exception gets propagated up (they both would contain equally useful information?).

    Not really. The exception in try could have been NullPointerException, OutOfMemoryError or actual I/O exception describing the nature of the problem. Even if it’s an I/O exception, the error could have left the stream in inconsistent state (or even closed it prematurely). Calling close() might yield completely different error, like “stream already closed“, “internal error” or whatever.

    One error in the system tends to cascade dozens of others, sometimes looking very unrelated. Always look for the first exception for the root cause. The rest is just garbage. A good example is an initialization that failed and left an object in inconsistent state. Later you see NullPointerExceptions everywhere, but the real problem occurred way earlier.

    If both other and close throw an exception, there are two unrelated problems. […] there isn’t any reason to suggest that the first exception caused the second (is there?), so either will do.

    Actually, both exceptions are imports, but the first one is probably more relevant. Solution? Use AutoCloseable resources in Java 7 and try-with-resources idiom. If exception occurs while cleaning up, it will be attached to original exception as suppressed:

    class Resource implements AutoCloseable  //...
    

    and then:

    try(Resource r = new Resource()) {
        r.use();
        other();
    }
    

    The exception will look something like this:

    MyException: Exception in use()
    at Resource.use(Main.java:11)
    at Main.main(Main.java:16)
        Suppressed: java.lang.RuntimeException: Exception in close()
            at Main.close(Main.java:6)
            at Main.main(Main.java:17)
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly sorry if this is a common question but I couldn't find anything on
Firstly I asked sorry if this is a stupid question. but I have a
Firstly, sorry about the question title, it may not be very descriptive for my
Firstly, very sorry if this is not a true stackoverflow question. But it's something
Firstly, sorry for the wording of the question. I'm not too sure how to
Firstly, sorry about the long question... but this is doing my head in. Any
Firstly, sorry for the difficult question title, but I can't think of anything better.
Firstly, Sorry For lengthy question but I have to give some underlying information. We
Firstly sorry for the long piece of code pasted below. This is my first
Firstly, sorry for my English (I'm from China). By reading this article( how to

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.