|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectde.unkrig.commons.lang.ExceptionUtil
public final class ExceptionUtil
Various Exception-related utility methods.
| Method Summary | ||
|---|---|---|
static
|
throW(EX throwable)
Identical with " throw throwable", but has a return type T, so it can be used in an expression. |
|
static
|
throwAssertionError(java.lang.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(java.lang.Exception e)
Throws the given Exception, although it does not declare any exceptions. |
|
static
|
wrap(java.lang.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
|
wrap(java.lang.String prefix,
java.lang.Throwable cause,
java.lang.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. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <T extends java.lang.Throwable> T wrap(@Nullable
java.lang.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 wrap
public static <T extends java.lang.Throwable> T wrap(@Nullable
java.lang.String prefix,
java.lang.Throwable cause,
java.lang.Class<T> wrapperClass)
prefix, a colon, a space, and the cause.
prefix - The text to prepend to the cause throwable's detail messagecause - The throwable to wrapwrapperClass - The type of the wrapping throwable
public static void throwUndeclared(java.lang.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 thrown
public static <T,EX extends java.lang.Throwable> T throW(EX throwable)
throws EX extends java.lang.Throwable
throw throwable", but has a return type T, so it can be used in an expression.
EX extends java.lang.Throwablepublic static <T> T throwAssertionError(java.lang.Object object)
throw new AssertionError(object)", but has a return type T, so it can be used
in an expression.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||