
net.yetamine.lang.Throwables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of net.yetamine.lang Show documentation
Show all versions of net.yetamine.lang Show documentation
Small extensions for the core Java language libraries.
package net.yetamine.lang;
/**
* A utility class for dealing with exceptions.
*/
public final class Throwables {
/**
* Calls {@link Throwable#initCause(Throwable)} on the given exception and
* returns the exception then.
*
*
* This method is suitable for compact initialization of the exceptions that
* have no cause-chaining constructor:
*
*
* try {
* // Some code that might raise an IOException
* } catch (IOException e) {
* throw Throwables.init(new NoSuchElementException(), e);
* }
*
*
* To detect wrong callers that supply {@code null} exception to initialize,
* causing an unexpected {@link NullPointerException}, the method checks the
* argument with an {@code assert}.
*
* @param
* the type of the exception to process
* @param t
* the exception to process. It must not be {@code null}.
* @param cause
* the cause to be set
*
* @return the given exception
*/
public static X init(X t, Throwable cause) {
assert (t != null);
t.initCause(cause);
return t;
}
private Throwables() {
throw new AssertionError();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy