Search |
||
Don't Try This at HomePosted by crazybob on September 11, 2004 at 7:44 AM PDT
Dr. Josh Bloch and Dr. Neal Gafter (Click and Hack) guest starred on Mary's Friday Free Stuff Puzzler last week. Every week, Mary, a Sun marketing geek, boxes up random items she finds around her office and ships them off to the weekly winner. Her blog is a fun read. Check it out.
Click and Hack provided an example of subverting compiler exception checking (i.e. throwing a checked exception like an unchecked exception) using the deprecated
public class Thrower {
public static void sneakyThrow(Throwable t) {
Thread.currentThread().stop(t); // deprecated
}
}
See for yourself by running this example program:
public class Test {
public static void main(String[] args) {
Thrower.sneakyThrow(new Exception("Ouch"));
}
}
The first solution takes advantage of a bad design decision in the
public class Thrower {
private static Throwable throwable;
private Thrower() throws Throwable {
throw throwable;
}
public static synchronized void sneakyThrow(Throwable throwable) {
// can't handle these types.
if (throwable instanceof IllegalAccessException
|| throwable instanceof InstantiationException)
throw new IllegalArgumentException();
Thrower.throwable = throwable;
try {
Thrower.class.newInstance();
}
catch (InstantiationException e) {
// can't happen.
e.printStackTrace();
}
catch (IllegalAccessException e) {
// can't happen.
e.printStackTrace();
}
finally {
Thrower.throwable = null;
}
}
}
Note that the code sets the static The final JDK 1.5 specific solution takes advantage of the fact that the compiler does not type check generics at runtime. In defense of JDK 1.5, the compiler does print a warning. public class TigerThrower »
Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|