
de.tsl2.nano.modelkit.ExceptionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.modelkit Show documentation
Show all versions of tsl2.nano.modelkit Show documentation
TSL2 Framework to provide and use a structure of elements referenced by unique names - to declare a kit of logic in a json/yaml/xml text file
The newest version!
/*
* File: $HeadURL$
* Id : $Id$
*
* created by: Tom
* created on: 31.03.2017
*
* Copyright: (c) Thomas Schneider 2017, all rights reserved
*/
package de.tsl2.nano.modelkit;
import java.util.Arrays;
import java.util.function.Supplier;
public class ExceptionHandler {
private ExceptionHandler() {
}
@SuppressWarnings("unchecked")
public static final T trY(final SupplierEx s, final Class extends Exception>... handleExceptions) {
try {
return s.get();
} catch (final Exception e) {
if (!Arrays.asList(handleExceptions).contains(cause(e).getClass())) {
return (T) forward(e);
} else {
return null;
}
}
}
public static Object forward(final Exception e) {
throw (RuntimeException) (e instanceof RuntimeException ? e : new RuntimeException(e));
}
private static Throwable cause(final Throwable e) {
if (e.getCause() != null) {
return cause(e.getCause());
}
return e;
}
@FunctionalInterface
public interface SupplierEx extends Supplier {
T getEx() throws Exception;
@Override
default T get() {
try {
return getEx();
} catch (final Exception e) {
throw new IllegalStateException(e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy