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 4231578
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:59:15+00:00 2026-05-21T01:59:15+00:00

Just a note : I know that in Java finally block should never throw

  • 0

Just a note:
I know that in Java finally block should never throw exceptions, otherwise it’s [very, very, very] bad practice.
I know that I should use try-catch inside finally to handle (e.g. log or ignore) all exceptions and prevent them from propagating.
I know that Java 7’s Throwable has getSuppressed method, but I target Java 5 & 6.

Problem: In Java, in try-finally if an exception (name it A) is thrown by try block, control reaches finally block (in case of no exception it reaches it also, but it’s not interesting in this question). Then, if finally block throws an exception (name it B), exception A is suppressed, or masked/swallowed, and exception B is propagated to the caller.

Question: Can i somehow detect the situation when one exception is suppressed by another and log/record the first one?
… I spent too much time reasoning why particular exception has been thrown and not knowing what has really happened.

Rationale: Often problematic try-finally blocks are coded in a library (today it was Hibernate), so I’m not able to modify them.

Solution constraints: As I noted at the beginning, possible solution should not relay on Java 7, but, on the other hand, does not need to be production grade (being such would be a bonus). AOP is an option here.

(Please don’t post trivial answers like “use Java 7 for development”:)

  • 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-05-21T01:59:15+00:00Added an answer on May 21, 2026 at 1:59 am

    The problem I think is that even with AOP you can not intercept the exception consumption process. With AOP you could capture, say, all of the exceptions that are created, but you can’t know when they’re consumed.

    For example:

    try {
        ...
    } catch (Exception e) {
        log.boom("Ouchies happened here", e);
    }
    

    Not every exception is re-thrown.

    However, at a certain level MOST exception ARE, eventually, thrown so that the caller can handled them.

    So, given that you will likely “leak” exceptions anyway, the game is to try and find the “leaking” exceptions.

    Using AOP you can while away each Exception as it is created (whether you can do this at the Exception level, or have to do it at the individual class level, I can’t say — not that familiar with AOP in practice in this case).

    Once you’re capturing exceptions as they are created, then you start wrapping method calls via AOP. When you do this, you can capture the exceptions being thrown by the method.

    So, with a little work, you know when returning from a method a) which exceptions were created and b) which were thrown. If you walk the exception “caused by” tree of the returned exception from the method, you can throw these out of the “exceptions created by this method” list. The remainder are the leaks.

    Ideally, after a little analysis, you’ll be able to identify which ones are simply not thrown (but handled in some other way), and which ones are actually shadowed by your finally blocks.

    It’s imperfect, and most certainly I wouldn’t use it in production (I’d simply have to put to much work in to all of the race condition around recording stuff and such. More work than I’d want to do for this kind of debugging, frankly). But it might give you the information you are looking for, especially when used sparingly across a small domain of classes.

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

Sidebar

Related Questions

Note that this function does not have a { and } body. Just a
This is probably obvious, so bear with me. YES, I KNOW THAT java.io.File has
I know that GUI code in Java Swing must be put inside SwingUtilities.invokeAndWait or
Note: I am just consuming webservice I have no control over webservice code. So
Note I'm running windows, the path just looks like it's linus because I typed
NOTE: I am not set on using VI, it is just the first thing
NOTE : I mention the next couple of paragraphs as background. If you just
In a comment I read Just as a side note, it's sometimes faster to
Just looking for the first step basic solution here that keeps the honest people
I understand that OSGI is just a specification and they don't event mandate that

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.