Skip to content

Instantly share code, notes, and snippets.

@killme2008
Created June 27, 2012 10:13
Show Gist options
  • Save killme2008/3003145 to your computer and use it in GitHub Desktop.
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.
/**
* 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