11
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.

Dan Burton
  • 53,238
  • 27
  • 117
  • 198
  • 2
    My intuition is telling me they're the same; but and therefore first is redundant, unless you wanted to log something specifically (ie the class name) before it was thrown; or maybe throw a custom exception. – Dave May 04 '11 at 21:01
  • The first one could also be an IDE auto generated catch clause which was never removed even though the method was declared to be throwing the same exception. But as stated in the answers both do the same thing. – Pratik Bhatt May 04 '11 at 23:13

5 Answers5

7

They are the same. I would use the second version.

Michael Barker
  • 14,153
  • 4
  • 48
  • 55
5

Yes. Since your method already throws "Exception", you dont need to catch it and re-throw it. Unless you want do what @Dave mentioned.

Kal
  • 24,724
  • 7
  • 65
  • 65
3

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.

Nathan Ryan
  • 12,893
  • 4
  • 26
  • 37
  • +1 and accept. Although Michael's answer was solid and simple, this one provides interesting details. Do you have any links where I could read more about Java's exception table generation? – Dan Burton May 04 '11 at 22:34
  • 1
    The JVM Specification (http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#9934) is a good place to start. The linked section provides details on the structure of the bytecode generated by compiling various forms of `try` statements. – Nathan Ryan May 05 '11 at 01:29
2

As you are throwing the exception you have caught in the catch clause and doing nothing else but throwing it again, yes both code will do the same thing.

fmucar
  • 14,361
  • 2
  • 45
  • 50
1

You are right, it is the same. Even the stacktrace will be there :)

But similar code might be use if you wrap the exception into some higher level one. If the code looks exactly like stated it is really pointless.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118