Utility class that re-throws a throwable as unchecked?

假如想象 提交于 2019-11-27 18:34:43

问题


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:


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.




回答2:


The great Guava library has a Method Throwables.propagate(Throwable) that does exactly what your code is doing: JavaDoc

From the doc:

Propagates throwable as-is if it is an instance of RuntimeException or Error, or else as a last resort, wraps it in a RuntimeException then propagates.




回答3:


Perhaps setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler) can help you. There you can define a global exception handler for all exceptions that occur.



来源:https://stackoverflow.com/questions/13528624/utility-class-that-re-throws-a-throwable-as-unchecked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!