Created
June 27, 2012 10:13
-
-
Save killme2008/3003145 to your computer and use it in GitHub Desktop.
Java throw even checked exceptions without being required to declare them or catch them.
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
/** | |
* Throw even checked exceptions without being required | |
* to declare them or catch them. Suggested idiom: | |
* <p> | |
* <code>throw sneakyThrow( some exception );</code> | |
*/ | |
static public RuntimeException sneakyThrow(Throwable t) { | |
// http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html | |
if (t == null) | |
throw new NullPointerException(); | |
Util.<RuntimeException>sneakyThrow0(t); | |
return null; | |
} | |
@SuppressWarnings("unchecked") | |
static private <T extends Throwable> void sneakyThrow0(Throwable t) throws T { | |
throw (T) t; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment