public Foo doDangerousStuff() throws Exception {
try {
dangerousMethod();
return new Foo();
} catch (Exception e) {
throw e;
} finally {
mustBeCalledAfterDangerousMethod();
}
}
Does this behave any differently than if we were to omit the catch clause?
public Foo doDangerousStuff() throws Exception {
try {
dangerousMethod();
return new Foo();
} finally {
mustBeCalledAfterDangerousMethod();
}
}
[edit] To clear the confusion, yes, the catch
block does nothing except re-throw the exception. I was wondering if this caused some sort of different ordering in when the finally
block is invoked (assume that the thrown exception is caught by the caller), but from what I infer from the answers thusfar, it does not.
Though the two source codes represent the same execution sequence, they will result in different bytecode. The first routine will have an exception table, for example, whereas the second will not. Their bytecode will have the same effect during execution, in the absence of instrumentation. It is possible that these methods behave differently if the bytecode is instrumented after compilation, or if the caught exception is of a type whose classfile is not available during execution.