public class Exceptions
extends java.lang.Object
Constructor and Description |
---|
Exceptions() |
Modifier and Type | Method and Description |
---|---|
static void |
throwUnchecked(java.lang.Throwable ex) |
static <T> T |
throwUnchecked(java.lang.Throwable ex,
java.lang.Class<T> returnType)
Because this method throws an unchecked exception, when it is called in a method with a return type the compiler
does not know the method is exiting, requiring a further line to return null or throw an unchecked exception
directly.
|
public static <T> T throwUnchecked(java.lang.Throwable ex, java.lang.Class<T> returnType)
String someMethod() { try { somethingThatThrowsException(); } catch (Exception e) { return throwUnchecked(e, String.class); // does not actually return, throws the exception } }
ex
- The exception that will be thrown, unwrapped and uncheckedreturnType
- trick to persuade the compiler that a method returns appropriatelypublic static void throwUnchecked(java.lang.Throwable ex)
ex
- The exception that will be thrown, unwrapped and unchecked