Created
December 16, 2014 18:08
-
-
Save nwillems/1e727446d6ae8d8e15a9 to your computer and use it in GitHub Desktop.
A Java exception wrapping method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Demonstrate usage | |
class Mainer{ | |
public static void main(String[] args){ | |
Boolean b = exceptionJoe(new Callable<Boolean>{ | |
public Boolean call(){ | |
return Boolean.TRUE; | |
} | |
}, Boolean.FALSE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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