Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Last active December 28, 2015 02:59
Show Gist options
  • Save Tarrasch/7431590 to your computer and use it in GitHub Desktop.
Save Tarrasch/7431590 to your computer and use it in GitHub Desktop.
Throwing in java - When is it a re-throw?
public class InOut {
public static void main(String[] args) {
insideSame();
insideNew();
outsideSame();
outsideNew();
}
public static abstract class CatchPrint {
public abstract void body() throws Exception;
public void run() {
try {
body();
} catch(Exception e) {
e.printStackTrace();
}
}
CatchPrint() {
run();
}
}
public static void insideSame() {
new CatchPrint() {
public void body() {
try {
divByZero();
} catch (Exception e) {
throw e;
}
}
};
}
public static void insideNew() {
new CatchPrint() {
public void body() throws Exception {
try {
divByZero();
} catch (Exception e) {
throw new Exception("Oh noes, I couldn't handle");
}
}
};
}
public static void outsideSame() {
new CatchPrint() {
public void body() throws Exception {
Exception e2 = new Exception("unreachable code");
try {
divByZero();
} catch (Exception e) {
e2 = e;
}
throw e2;
}
};
}
public static void outsideNew() {
new CatchPrint() {
public void body() throws Exception {
Exception e2 = new Exception("unreachable code");
try {
divByZero();
} catch (Exception e) {
e2 = e;
}
throw new Exception("This definetly is a new fresh throw!");
}
};
}
public static int divByZero() {
return 1/0;
}
}
/*
So apparently it will do a re-throw only when you throw the same exception, regardless of if the exception is thrown inside or outside the handler. Here's the output of the program. You should look for wheater if the top of the stack is containing a `divByZero` or not.
$ javac -version
javac 1.7.0_51
$ javac InOut.java
$ java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.13.10.1)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
$ java InOut
java.lang.ArithmeticException: / by zero
at InOut.divByZero(InOut.java:77)
at InOut$1.body(InOut.java:27)
at InOut$CatchPrint.run(InOut.java:13)
at InOut$CatchPrint.<init>(InOut.java:19)
at InOut$1.<init>(InOut.java:24)
at InOut.insideSame(InOut.java:24)
at InOut.main(InOut.java:3)
java.lang.Exception: Oh noes, I couldn't handle
at InOut$2.body(InOut.java:41)
at InOut$CatchPrint.run(InOut.java:13)
at InOut$CatchPrint.<init>(InOut.java:19)
at InOut$2.<init>(InOut.java:36)
at InOut.insideNew(InOut.java:36)
at InOut.main(InOut.java:4)
java.lang.ArithmeticException: / by zero
at InOut.divByZero(InOut.java:77)
at InOut$3.body(InOut.java:52)
at InOut$CatchPrint.run(InOut.java:13)
at InOut$CatchPrint.<init>(InOut.java:19)
at InOut$3.<init>(InOut.java:48)
at InOut.outsideSame(InOut.java:48)
at InOut.main(InOut.java:5)
java.lang.Exception: This definetly is a new fresh throw!
at InOut$4.body(InOut.java:70)
at InOut$CatchPrint.run(InOut.java:13)
at InOut$CatchPrint.<init>(InOut.java:19)
at InOut$4.<init>(InOut.java:62)
at InOut.outsideNew(InOut.java:62)
at InOut.main(InOut.java:6)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment