sneaky throwing

Uwe Schindler uschindler at
Fri Jan 10 00:18:22 PST 2014

Hi Andy,


This is indeed the wrong mailing list! Maybe I help to answer your question: There are several ways to „sneaky throw“ checked Exceptions, but your approach looks very complicated.


In fact, the JVM does not check “throws” clauses at runtime. It does not matter to the JVM if it is a checked or RuntimException/Error whatever (see also Java Language Spec). The “throws” clause is just a hint for the compiler, so it can complain if an Exception is not correctly catched. Your approach seems to also use this, but does use some precompiled interface classes in two different versions to make compiler happy.


Two easy ways to throw checked Exceptions without declaring them are:

-          The way that simply tricks out the compiler is: or (these tricks use generics to declare Exceptions - which are lost when the Java compiler erasures the generic type). Please note, this approach is not a bug in the compiler and works with every JVM 1.5+! The first code part is taken from a book from Joshua Bloch: !

-          Thread.currentThread().stop(exception); // this is the “oldest way” and works since early beginning, but completely misuses already deprecated methods. It also depends on native methods, that are per my description above not needed. So don’t use!!!


The first Lucene/JavaPuzzlers-based approach works definitely also with Java 8 (why should it not, its all according to language specs?). We are running that in our test cases day-by-day with Java 8. The second approach is risky and may no longer work with later JVMs.





Uwe Schindler

uschindler at 

Apache Lucene PMC Chair / Committer

Bremen, Germany


From: hotspot-compiler-dev-bounces at [mailto:hotspot-compiler-dev-bounces at] On Behalf Of Andy Nuss
Sent: Friday, January 10, 2014 5:39 AM
To: hotspot
Subject: sneaky throwing




I had a lot of reasons to sneaky throw exceptions just as they are but without having the function that throws them declare the exception.  This saves me alot of trouble, especially with reflective errors that I know shouldn't ever happen.


To do this, I declared an interface that throws Throwable and the throwing function taking a single argument, the Throwable, and just providing a simple implementation for it.  Compiled to a .class and saved the byte code file as a small hex string.  Meanwhile, I declared another version of the interface that is identical in every way except that it does not declare that its throwing function throws Throwable.  I then use the saved bytecodes as the implementation for this interface, and use a singleton to provide the public static function.  Apparently, at the bytecode level, this is ok.


This has worked nicely all the way from JDK4 to JDK7.  Will it work with JDK8?  For example, lets say I have a tomcat servlet which throws 2 kinds of declared exceptions.  But I in fact sneaky throw many others during development.  This is ok right now.



-------------- next part --------------
An HTML attachment was scrubbed...

More information about the hotspot-compiler-dev mailing list