Skip to content

Instantly share code, notes, and snippets.

@nwillems
Created December 16, 2014 18:08
Show Gist options
  • Save nwillems/1e727446d6ae8d8e15a9 to your computer and use it in GitHub Desktop.
Save nwillems/1e727446d6ae8d8e15a9 to your computer and use it in GitHub Desktop.
A Java exception wrapping method
// Demonstrate usage
class Mainer{
public static void main(String[] args){
Boolean b = exceptionJoe(new Callable<Boolean>{
public Boolean call(){
return Boolean.TRUE;
}
}, Boolean.FALSE);
}
}
public class Utils{
/**
* Wraps a call with some exception handling stuffs
* @param call the Callable doing the exception throwing logic
* @param errorVal the value to return if the Callable throws an exception
* @return either the value from the Callable or the errorVal
*/
public static <T> T exceptionJoe(Callable<T> call, T errorVal){
try{
T result = call.call();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return errorVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment