
org.leialearns.utilities.ExceptionWrapper Maven / Gradle / Ivy
package org.leialearns.utilities;
/**
* Provides a static function to wrap a throwable in a RuntimeException if necessary.
*
* This can be used instead of declaring checked exceptions, like this:
*
*
* try {
* f = open("/dev/null", "w");
* } catch (Throwable throwable) {
* throw ExceptionWrapper.wrap(throwable);
* }
*
*
*/
public class ExceptionWrapper {
private ExceptionWrapper() {
throw new UnsupportedOperationException("This class must not be instantiated: " + getClass().getSimpleName());
}
/**
* Returns the given throwable as a RuntimeException
wrapping it in a new
* RuntimeException
if necessary.
* @param throwable The throwable to wrap
* @return The wrapped throwable
*/
public static RuntimeException wrap(Throwable throwable) {
RuntimeException result;
if (throwable instanceof RuntimeException) {
result = (RuntimeException) throwable;
} else {
result = new RuntimeException(throwable.getClass().getSimpleName(), throwable);
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy