com.ajjpj.abase.util.AUnchecker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
The newest version!
package com.ajjpj.abase.util;
import com.ajjpj.abase.function.AFunction0;
import com.ajjpj.abase.function.AStatement0;
/**
* This class throws an arbitrary exception without requiring it to be declared in a throws clause
*
* @author arno
*/
public class AUnchecker {
public static void throwUnchecked(Throwable th) {
AUnchecker. doIt(th);
}
@SuppressWarnings("unchecked")
private static void doIt(Throwable th) throws T {
throw (T) th;
}
@SuppressWarnings("unused")
public static void executeUnchecked(AStatement0 extends Exception> callback) {
try {
callback.apply();
}
catch(Exception exc) {
throwUnchecked (exc);
}
}
public static R executeUnchecked(AFunction0 callback) {
try {
return callback.apply();
}
catch(Exception exc) {
throwUnchecked (exc);
return null; //for the compiler
}
}
}