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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:32:41+00:00 2026-06-11T15:32:41+00:00

Suppose i have a method that may fail with a checked exception (checked because

  • 0

Suppose i have a method that may fail with a checked exception (checked because it is recoverable, according to Sun’s recommendations).
This method fails and it triggers a recovery strategy.
But both the initial method and the recovery strategy failed.

In some cases i may want to have both stacktraces so that i know why both the initial and recovery strategies failed, and not only the last one.

What can i do?
Should i create a CompositeException type or something like that? Is it a good practice?

  • 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-11T15:32:42+00:00Added an answer on June 11, 2026 at 3:32 pm

    Java 7 has introduced the concept of a suppressed exception. For instance, the try-with-resources statement is specified by:

    Resources are closed in the reverse order from that in which they were initialized. A resource is closed only if it initialized to a non-null value. An exception from the closing of one resource does not prevent the closing of other resources. Such an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource.

    and

    • If the initialization of the resource completes normally, and the try block completes abruptly because of a throw of a value V, then:

      • If the automatic closing of the resource completes abruptly because of a throw of a value V2, then the try-with-resources statement completes abruptly because of a throw of value V with V2 added to the suppressed exception list of V.

    This uses java.lang.Throwable.addSuppressedException, whose javadoc reads:

    Appends the specified exception to the exceptions that were suppressed in order to deliver this exception. This method is thread-safe and typically called (automatically and implicitly) by the try-with-resources statement.

    The suppression behavior is enabled unless disabled via a constructor. When suppression is disabled, this method does nothing other than to validate its argument.

    Note that when one exception causes another exception, the first exception is usually caught and then the second exception is thrown in response. In other words, there is a causal connection between the two exceptions. In contrast, there are situations where two independent exceptions can be thrown in sibling code blocks, in particular in the try block of a try-with-resources statement and the compiler-generated finally block which closes the resource. In these situations, only one of the thrown exceptions can be propagated. In the try-with-resources statement, when there are two such exceptions, the exception originating from the try block is propagated and the exception from the finally block is added to the list of exceptions suppressed by the exception from the try block. As an exception unwinds the stack, it can accumulate multiple suppressed exceptions.

    An exception may have suppressed exceptions while also being caused by another exception. Whether or not an exception has a cause is semantically known at the time of its creation, unlike whether or not an exception will suppress other exceptions which is typically only determined after an exception is thrown.

    Note that programmer written code is also able to take advantage of calling this method in situations where there are multiple sibling exceptions and only one can be propagated.

    That last paragraph seems to apply to your situation. So you could do:

    public static void main(String[] args) throws Exception {
        try {
            throw new RuntimeException("Not now!");
        } catch (Exception e) {
            try {
                tryAgain();
            } catch (Exception e2) {
                e.addSuppressed(e2);
                throw e;
            }
        }
    }
    

    Then, the stack trace will contain both exceptions:

    Exception in thread "main" java.lang.RuntimeException: Not now!
        at tools.Test.main(Test.java:12)
        Suppressed: java.lang.RuntimeException: I'm on holiday.
            at tools.Test.tryAgain(Test.java:7)
            at tools.Test.main(Test.java:15)
    

    but only the primary exception can be caught by the caller.

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

Sidebar

Related Questions

Suppose I have a Java method that returns a HashMap object. Because a LinkedHashMap
Suppose that I have a method called doSomething() and I want to use this
Suppose I have a method that must return an object (say from a database
Suppose I have a static method of my class that returns an object of
Suppose I have a pure virtual method in the base interface that returns to
Suppose that we have a class called class1. The class1 has a method called
Suppose I have a number of related classes that all have a method like
In Rails 3.0.9 (Ruby 1.9.2, MySQL) I have a method that is supposed to
I have the following method that is supposed to be a generic Save to
Suppose I have a big program that consists of hundreds of methods in it.

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.