May 16, 2009

Java Traps: return in a finally clause

The following code does not throw RuntimeException -- it is lost....


public class MainApp {
public static void main(String[] args) {
try {
produceRuntimeException();
System.out.println("yikes!!!");
} catch (Exception ex) {
System.err.println("Got it!" );
}

}

private static void produceRuntimeException() {
try {
throw new RuntimeException();
} finally {
return;
}
}
}