Community Page
- www.brodwall.com/johannes/blog/ Jump to website »
-
Subscribe -
Community
-
Top Commenters
-
Popular Threads
-
Recent Comments
- The evolution of SOA Introduce the concepts of services and SOA Design principles of SOA ... The benefits of employing SOA Review of common business goals ... Related articles. Web Application...
- Great article and I agree with you that ............ Thanks for the tips!
- Great read, good work old chap :)
- Hi...Your post really got me thinking man..... an intelligent piece ,I must say.
- Was a good read. thank great post, I think this article is useful. I'll be back for more. Thanks for sharing the information . .. :)
Jump to original thread »
Anders NordÃ¥s shows how to throw a checked exception without declaring a throws clause. The method uses some inherently evil mechanisms (the name of the class “sun.misc.Unsafe” should be a tip of), and like Anders says, this should probably never be used in produ
... Continue reading »
1 year ago
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
1 year ago
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.