All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.tsl2.nano.modelkit.ExceptionHandler Maven / Gradle / Ivy

Go to download

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!
package de.tsl2.nano.modelkit;

import java.util.Arrays;
import java.util.function.Supplier;

/**
 * @author scne522
 */
public class ExceptionHandler {
    private ExceptionHandler() {
    }

    @SuppressWarnings("unchecked")
    public static final  T trY(final SupplierEx s, final Class... 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