| Modifier and Type | Method and Description |
|---|---|
static <T,EX extends Throwable> |
throW(EX throwable)
Identical with "
throw throwable", but has a return type T, so it can be used in an expression. |
static <T> T |
throwAssertionError(Object object)
Identical with "
throw new AssertionError(object)", but has a return type T, so it can be used
in an expression. |
static void |
throwUndeclared(Exception e)
Throws the given
Exception, although it does not declare any exceptions. |
static <T extends Throwable> |
wrap(String prefix,
T cause)
Wraps a given 'cause' in another throwable of the same type, with a detail message composed from
prefix, a colon, a space, and the cause. |
static <T extends Throwable> |
wrap(String prefix,
Throwable cause,
Class<T> wrapperClass)
Wraps a given 'cause' in another throwable of the given wrapper class type, with a detail message composed
from prefix, a colon, a space, and the cause.
|
public static <T extends Throwable> T wrap(@Nullable String prefix, T cause)
prefix, a colon, a space, and the cause.
This is useful for adding context information to a throwable, e.g. which file is currently being processed, the current line number, etc.
prefix - The text to prepend to the cause throwable's detail messagecause - The throwable to wrappublic static <T extends Throwable> T wrap(@Nullable String prefix, Throwable cause, Class<T> wrapperClass)
prefix - The text to prepend to the cause throwable's detail messagecause - The throwable to wrapwrapperClass - The type of the wrapping throwablepublic static void throwUndeclared(Exception e)
Exception, although it does not declare any exceptions.
This feature exploits the fact that the per-method declaration of thrown exceptions (the THROWS clause) is a compiler feature and not a runtime feature, and can be circumvented with some trickery.
This is useful e.g. for implementations of Iterator, Runnable and other "service classes"
that want to throw checked exceptions. (Wrapping these in RuntimeExceptions is
sometimes not an option.)
Notice that the only way to catch such undeclared checked exceptions is "try { ... } catch (Exception e) { ... }".
Notice that this method breaks Java's concept of exception checking entirely.
It also renders the exception-type-parametrized interfaces *WhichThrows (e.g. RunnableWhichThrows) useless. One should use one or the other, but never both.
Usage example:
import static de.unkrig.commons.lang.ExceptionUtil.throwUndeclared;
class MyIterator<E> implements Iterator<E> {
public E next() {
// ...
throwUndeclared(new IOException());
}
// ...
}e - The exception to be thrownpublic static <T,EX extends Throwable> T throW(EX throwable) throws EX extends Throwable
throw throwable", but has a return type T, so it can be used in an expression.EX extends Throwablepublic static <T> T throwAssertionError(Object object)
throw new AssertionError(object)", but has a return type T, so it can be used
in an expression.Copyright © 2018 Arno Unkrig. All rights reserved.