/ blog / java / friday-free-stuff.html

Root Beer Logo Root Beer

Chris Nokleberg's Fizzy Weblog

September 2004
Su M Tu W Th F Sa
      1 2 3 4
5 6 7 8 9 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Previous  |  Next  |  More...
#  Friday Free Stuff

I just came across the Friday Free Stuff Java puzzle contest run by MaryMaryQuiteContrary. Unfortunately I noticed it after this week's contest had ended (congrats crazybob), but maybe I still have a shot at the bonus points [Emoticon]

The puzzle was to write a static method without a throws clause that would throw any exception you pass to it, including checked exceptions. Since the checked/unchecked exception distinction is not enforced by the JVM (it is a Java language feature), all you really have to do is get the right bytecode loaded. In the following solution, a class implementing the Mirror interface is loaded from a byte array. The single method in the interface does not declare any exceptions, but the sneaky implementation will throw any Throwable you pass to it anyway:

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class Thrower {
    public static void sneakyThrow(Throwable t) {

    interface Mirror {
        void reflect(Throwable t);

    private static Mirror mirror = makeMirror();

    private static Mirror makeMirror() {
        try {
            byte[] bytes = new byte[]{

            Class[] types = new Class[]{

            Object[] args = new Object[]{
                new Integer(0),
                new Integer(bytes.length),

            Method m = ClassLoader.class.getDeclaredMethod("defineClass", types);
            ClassLoader cl = Thrower.class.getClassLoader();
            return (Mirror)((Class)m.invoke(cl, args)).newInstance();

        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);

The bytecode itself is very straightforward, as you can see from this "javap -c" output:

Compiled from "<generated>"
public class ThrowerImpl extends java.lang.Object implements Thrower$Mirror{
public ThrowerImpl();
   0:	aload_0
   1:	invokespecial	#12; //Method java/lang/Object."<init>": ()V
   4:	return

public void reflect(java.lang.Throwable);
   0:	aload_1
   1:	athrow
   2:	return

The real trick, of course, is crafting the bytecode, since javac won't let you do it. Thankfully, nowadays there are plenty of other tools, such as ASM. Click here to see the code I used; it should print out text suitable for cutting-and-pasting into the array initializer. This code could easily be inlined into the Thrower implementation, but I didn't want to have any non-JDK dependencies.

One advantage of this solution is that any Throwable can be thrown, including IllegalAccessException and InstantiationException.

Postscript: After I had finished writing this I discovered that other people had tried to submit this JavaWorld article as a solution, which describes a technique almost identical to the one here. The difference is that the bytecode is created as a result of modifying an existing classfile via disassembly, instead of being inlined or generated on the fly. The article's solution was turned down because the judges were "looking for code that you can just compile and use, no cheap modify-the-class-file or create-a-new-class-file after-compiling-the-client tricks," but I think that's a bit harsh given it should be clear that was just due to a particular implementation. Actually, they probably just wanted to give free stuff to people who know something about Java, instead of being good at searching for JavaWorld articles. [Emoticon]

[Powered By FreeMarker]  [Valid Atom 1.0]  [Weblog Commenting by]