7

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?

Mr. White
  • 109
  • 1
  • 3
  • 7
    The 2 code samples are not equivalent. – krock Jul 27 '10 at 03:32
  • 1
    Yeah in the second situation you certainly don't want the `break;` Either your program won't compile, or you'll be breaking out of the wrong loop. –  Jul 27 '10 at 03:36
  • As others have noted, the code examples are not equivalent. If you're not in a loop, you can't really brake from it. Regardless though, what is stopping you from benchmarking it? It's fairly simple code to benchmark. – Wolph Jul 27 '10 at 03:38
  • `@krock:` I was waiting for someone to say this; at a high-level, it's equivalent in my code. I'll post a complete code example later, but hopefully you understood the question. – Mr. White Jul 27 '10 at 03:38
  • `@all:` The second break was a mistake from copy/paste. – Mr. White Jul 27 '10 at 03:42

5 Answers5

4

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.

Community
  • 1
  • 1
AaronM
  • 1,577
  • 12
  • 15
2

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.

0

The placement of your try-catch has no incidence whatsoever on the performance of your application. Even if it did, it would be completely negligible, that's not where you want to direct your energy. Implement based on need first, and then optimize. Don't micro-optimize.

-1

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
  • 118,147
  • 33
  • 203
  • 236
-1

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

Mr. White
  • 109
  • 1
  • 3