sneaky throwing
Uwe Schindler
uschindler at apache.org
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: http://fossies.org/linux/www/lucene-4.6.0-src.tgz:a/lucene-4.6.0/test-framework/src/java/org/apache/lucene/util/Rethrow.java or http://java.dzone.com/articles/throw-checked-exceptions (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: http://www.javapuzzlers.com !
- 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
-----
Uwe Schindler
uschindler at apache.org
Apache Lucene PMC Chair / Committer
Bremen, Germany
http://lucene.apache.org/
From: hotspot-compiler-dev-bounces at openjdk.java.net [mailto:hotspot-compiler-dev-bounces at openjdk.java.net] On Behalf Of Andy Nuss
Sent: Friday, January 10, 2014 5:39 AM
To: hotspot
Subject: sneaky throwing
Hi,
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.
Andy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20140110/6ecf013c/attachment-0001.html
More information about the hotspot-compiler-dev
mailing list