Skip to content

Commit e381373

Browse files
bpsmrichhickey
authored andcommitted
CLJ-855: Util.sneakyThrow() throws any exception without being required to declare or catch it
Signed-off-by: Rich Hickey <[email protected]>
1 parent dfe20f6 commit e381373

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/jvm/clojure/lang/Util.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,24 @@ static public RuntimeException runtimeException(Throwable e){
179179
return new RuntimeException(e);
180180
}
181181

182+
/**
183+
* Throw even checked exceptions without being required
184+
* to declare them or catch them. Suggested idiom:
185+
* <p>
186+
* <code>throw sneakyThrow( some exception );</code>
187+
*/
188+
static public RuntimeException sneakyThrow(Throwable t) {
189+
// http://www.mail-archive.com/[email protected]/msg05984.html
190+
if (t == null)
191+
throw new NullPointerException();
192+
Util.<RuntimeException>sneakyThrow0(t);
193+
return null;
194+
}
195+
196+
@SuppressWarnings("unchecked")
197+
static private <T extends Throwable> void sneakyThrow0(Throwable t) throws T {
198+
throw (T) t;
182199
}
200+
201+
}
202+

0 commit comments

Comments
 (0)