![JAR search and dependency download from the Maven repository](/logo.png)
no.motif.Exceptions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of motif Show documentation
Show all versions of motif Show documentation
A library helping you to compose behavior from smaller parts.
The newest version!
package no.motif;
import no.motif.f.Fn;
/**
* Utilities for dealing with exceptions.
*/
public final class Exceptions {
/**
* If you need to rethrow checked exceptions as RuntimeException, this method will
* ensure a sensible RuntimeException being acquired. Typical use of this method is
* like this:
*
* try {
* someMethod(); // may throw checked Exception
* } catch (Exception e) {
* throw asRuntimeException(e);
* }
*
*
* @param e The exception.
* @return If the given exception is a RuntimeException, it will be returned as-is
* casted to RuntimeException, or else the exception will be wrapped in a
* new RuntimeException.
*/
public static RuntimeException asRuntimeException(Exception e) {
if (e instanceof RuntimeException) {
return (RuntimeException) e;
} else {
RuntimeException rtEx = new RuntimeException(e.getClass().getName() + ": " + e.getMessage(), e);
rtEx.setStackTrace(e.getStackTrace());
return rtEx;
}
}
/**
* Get the {@link Throwable#getMessage() message} of a Throwable
.
*/
public static final Fn message = new Fn() {
@Override public String $(Throwable throwable) { return throwable.getMessage(); }};
/**
* Get the {@link Throwable#getCause() cause} of a Throwable
.
*/
public static final Fn cause = new Fn() {
@Override public Throwable $(Throwable throwable) { return throwable.getCause(); }};
private Exceptions() {}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy