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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:33:39+00:00 2026-06-14T20:33:39+00:00

I am catching all throwables in a unit test wrapper method in order to

  • 0

I am catching all throwables in a unit test wrapper method in order to reset some data in an external system. I want to re-throw the original exception when this is done and I am using this piece of code to do it:

if (t instanceof RuntimeException) {
    throw (RuntimeException) t;
} else if (t instanceof Error) {
    throw (Error) t;
} else {
    throw new RuntimeException(t);
}

However, is there any existing utility call that does this already?

(I am catching throwables because AssertionErrors are Errors.)

Edit: To be honest, I don’t really want to wrap the exception, so any trick that would allow me to throw any throwable (including checked exceptions) without declaring throws is acceptable.

  • 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-14T20:33:41+00:00Added an answer on June 14, 2026 at 8:33 pm

    Yes, there is a way to write a method that will avoid wrapping your checked exceptions. For this use case it is a perfect match, although you should definitely be very careful with it, since it can easily confuse the uninitiated. Here it is:

    @SuppressWarnings("unchecked")
    public static <T extends Throwable> void sneakyThrow(Throwable t) throws T {
        throw (T) t;
    }
    

    and you’d use it as

    catch (Throwable t) { sneakyThrow(t); }
    

    As commented by Joachim Sauer, in certain cases it helps convince the compiler that the line calling sneakyThrow causes the method to terminate. We can just change the declared return type:

    @SuppressWarnings("unchecked")
    public static <T extends Throwable> T sneakyThrow(Throwable t) throws T {
        throw (T) t;
    }
    

    and use it like this:

    catch (Throwable t) { throw sneakyThrow(t); }
    

    For educational purposes it is nice to see what’s going on at the bytecode level. The relevant snippet from javap -verbose UncheckedThrower:

    public static <T extends java.lang.Throwable> java.lang.RuntimeException sneakyThrow(java.lang.Throwable) throws T;
      descriptor: (Ljava/lang/Throwable;)Ljava/lang/RuntimeException;
      flags: ACC_PUBLIC, ACC_STATIC
      Code:
        stack=1, locals=1, args_size=1
           0: aload_0
           1: athrow
      Exceptions:
        throws java.lang.Throwable
      Signature: #13                          // <T:Ljava/lang/Throwable;>(Ljava/lang/Throwable;)Ljava/lang/RuntimeException;^TT;
    

    Note there is no checkcast instruction. The method even legitimately declares to throw T, which can be any Throwable.

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

Sidebar

Related Questions

The Problem I'm running multiple invocations of some external method via an ExecutorService .
I'm catching all exceptions within: java.lang.Throwable /page.xhtml java.lang.Error /page.xhtml But what If I get
By referring to C++ catching all exceptions try { int i = 0; int
I had someone mention to me that catching all exceptions is not necessarily good
Based on this question I've created a small application which is catching all debug
We currently have an error catching script for all our PHP sites. This script
I'm catching exceptions in context managers, however I don't see all levels of reraised
In Java, is there any way to get (catch) all exceptions instead of catching
I am using Health Monitoring for catching all errors and sending them to email.
I have an interceptor for catching Exceptions and sending emails of this exceptions. All

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.