|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectch.javasoft.util.ExceptionUtil
public class ExceptionUtil
ExceptionUtil is a utility class with static helper methods for exception handling, for instance to convert exceptions to some desired exception type.
| Method Summary | ||
|---|---|---|
static IOException |
toIOException(String msg,
Throwable cause)
Jdk 1.6++ contains constructors with causes for IOException, but
older jdk versions do not. |
|
static IOException |
toIOException(Throwable cause)
Jdk 1.6++ contains constructors with causes for IOException, but
older jdk versions do not. |
|
static RuntimeException |
toRuntimeException(Throwable throwable)
Converts the given throwable into a RuntimeException, if
necessary, and returns it. |
|
static
|
toRuntimeExceptionOr(Class<T> exClass,
Throwable throwable)
Returns the given throwable if it is of the expected type and returns it, or converts it into a RuntimeException, if
necessary, and throws it. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static RuntimeException toRuntimeException(Throwable throwable)
throws Error
RuntimeException, if
necessary, and returns it.
If throwable already was a runtime exception, it is returned
as is. If throwable was an Error, it is rethrown. In
every other case, a new runtime exception is created, nesting the
original throwable
(see RuntimeException.RuntimeException(Throwable)).
The intended use of this method could somehow look like this:
void myMethod(...) throws RuntimeException {
try {
... //any kinds of exceptions might occur
}
catch(Throwable th) {
throw ExceptionUtil.toRuntimeException(th);
//you never get here (and the compiler knows this)
}
}
throwable - the given exception to convert, if necessary
RuntimeException nor an
Error
Error - if throwable was an instance of Error
public static <T extends Throwable> T toRuntimeExceptionOr(Class<T> exClass,
Throwable throwable)
throws Error,
RuntimeException
RuntimeException, if
necessary, and throws it.
If throwable is of the desired exception class, it is returned.
If not, an Error is thrown if throwable was such.
Otherwise, a RuntimeException is thrown, either the
throwable itself or a newly created RuntimeException
nesting throwable
(see RuntimeException.RuntimeException(Throwable)).
The intended use of this method could somehow look like this:
void myMethod(...) throws MyException {
try {
... //any kinds of exceptions might occur
}
catch(Throwable th) {
throw ExceptionUtil.toRuntimeExceptionOr(MyException.class, th);
//you never get here (and the compiler knows this)
}
}
throwable - the given exception to validate
Error - if throwable was such an instance
and exClass was Error not nor a super
class
RuntimeException - if throwable was such an instance
and exClass was not RuntimeException
nor a super class. If throwable was not an
instance of the expected exception class
exClass, a runtime exception is throw nesting
the original throwable.
public static IOException toIOException(String msg,
Throwable cause)
IOException, but
older jdk versions do not. This method simulates the missing constructor.
IOException,
Exception.Exception(String, Throwable)public static IOException toIOException(Throwable cause)
IOException, but
older jdk versions do not. This method simulates the missing constructor.
IOException,
Exception.Exception(Throwable)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||