-
Website
http://www.brodwall.com/johannes/blog/ -
Original page
http://www.brodwall.com/johannes/blog/2007/07/17/evil-behavior-with-unchecked-checked-exceptions/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
Andy Palmer
1 comment · 1 points
-
Affordable SEO Services
1 comment · 1 points
-
anderssv
3 comments · 1 points
-
eirikma
2 comments · 1 points
-
vidar
1 comment · 1 points
-
-
Popular Threads
-
Å trene på Java EE
3 weeks ago · 1 comment
-
Å trene på Java EE
for even more clever implementations. :-)
I propose a utility-class with something like this, and we're good to go:
public static void throwUnchecked(final Throwable pException) {
// Type parameter to Thrower is erased at compile-time
new Thrower().sneakyThrow(pException);
}
private static class Thrower {
private void sneakyThrow(Throwable pException) throws T {
throw (T) pException; // Unchecked cast
}
}
I'm starting to think this might be useful.. Or... Maybe I'm just tired.. :-P
.k
The code as it stands doesn't work. I expect the template code was eaten by the HTML-izer of both my blog and/or crazy bob's blog. Here is the correct code:
<pre>
</pre>public static void throwIt2(final Throwable exception) {
class Thrower<T extends Throwable> {
private void sneakyThrow(Throwable exception) throws T {
throw (T)exception;
}
}
new Thrower<RuntimeException>().sneakyThrow(exception);
}
I don't even get a compiler warning. This is a very neat trick.