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

net.amygdalum.testrecorder.runtime.Throwables Maven / Gradle / Ivy

The newest version!
package net.amygdalum.testrecorder.runtime;

public final class Throwables {
	
	private Throwables() {
	}

	public static Throwable capture(WithResult code) {
		return capture(code, Throwable.class);
	}
	
	public static  T capture(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 capture(WithoutResult code) {
		return capture(code, Throwable.class);
	}
	
	public static  T capture(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 - 2025 Weber Informatics LLC | Privacy Policy