views:

178

answers:

6

Does the placement of a try-catch block affect performance?

EXAMPLE 1: try-catch block inside of the while-loop

while (true) {
    try {
        // ... read from a file
    } catch (EOFException e) {
        break;
    }
}

EXAMPLE 2: try-catch block surrounds the while-loop

try {
    while (true) {
        // ... read from a file
    } 
} catch (EOFException e) {
    // :P
}

Logically, these two examples are equivalent, but which should I prefer?

+1  A: 

Whatever overhead try-catch incurs is probably negligible, but what draws my attention more with the first case is that it's misleading: you're catching an exception, only to abort the loop. I'd pick solution 2 just because it's consistent with the intent. And you avoid any overhead that way.

Phong
The second break was a mistake from copy/paste.
Mr. White
I didn't say anything about that. I knew it was a mistake and ignored it because you had clearly stated your intent.
Phong
A: 

can you break from outside the while? I don't think your presupposition, that the 2 are equivalent are true.

My intuition tells me that the try/catch outside the loop is better. If there is a bytecode impact by writing a try/catch, having less created is better. If there is no impact unless the exception occurs, then it doesn't matter. I don't see any circumstances where having the try/catch inside would be better.

I could be wrong...

hvgotcodes
The second break was a mistake from copy/paste.
Mr. White
+1  A: 

http://stackoverflow.com/questions/2633834/should-java-try-blocks-be-scoped-as-tightly-as-possible

This gives a much better answer than I could. Short of it is, they only add an entry onto a table that's checked when exceptions are thrown, so unless an exception is thrown they don't affect performance. It'd be best to just put it wherever makes it best to try recover, if you can. If not, wherever's useful or makes sense. Though with break outside the loop, I don't think the second is valid anyway.

AaronM
The second break was a mistake from copy/paste.
Mr. White
A: 

The second example (try-catch block surrounding the code) is both faster and clearer.

Mr. White
A: 

My intuition is that the two forms are virtually identical, performance wise.

  • In the case where no exceptions are thrown, the performance should be identical. The JIT compiler should optimize the code layout to minimize branches for the case where no exception is thrown. The location of the try catch block should therefore make no difference.

  • In the case where an exceptions are thrown and caught by the catch, I guess it is possible that one case may do one branch than the other. But in practice, we are only talking about 1 or 2 machine instructions, and that is trivial compared with the other costs of creating an exception, throwing it and working out where to catch it.

In short, don't worry. Choose the alternative that is going to be easiest for the next guy who maintains your code to understand.

Stephen C
I don't care too much about the next guy; I care about what's easiest for me. Otherwise, good advice.
Mr. White
Remember, a few months from now when you are reviewing old code, YOU are the next guy.
gregcase
@gregcase - even more so if it is someone else's old code.
Stephen C
A: 

Placement of try-catch-finally does not affect the performance in any way you should worry about. However, it does affect the control flow and what you would want to/can do in try and finally blocks.

Samit G.