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

net.amygdalum.extensions.hamcrest.exceptions.Exceptions Maven / Gradle / Ivy

package net.amygdalum.extensions.hamcrest.exceptions;

public final class Exceptions {
	
	private Exceptions() {
	}

	public static Throwable catchException(WithResult code) {
		return catchException(code, Throwable.class);
	}
	
	public static  T catchException(WithResult code, Class clazz) {
		try {
			code.run();
			return null;
		} catch (Throwable exception) {
			if (clazz.isInstance(exception)) {
				return clazz.cast(exception);
			} else {
				return null;
			}
		}
	}
	
	public static Throwable catchException(WithoutResult code) {
		return catchException(code, Throwable.class);
	}
	
	public static  T catchException(WithoutResult code, Class clazz) {
		try {
			code.run();
			return null;
		} catch (Throwable exception) {
			if (clazz.isInstance(exception)) {
				return clazz.cast(exception);
			} else {
				return null;
			}
		}
	}
	
	
	public interface WithResult {
		T run() throws Throwable;
	}
	
	public interface WithoutResult {
		void run() throws Throwable;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy