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?

5
  • 7
    The 2 code samples are not equivalent. Commented Jul 27, 2010 at 3: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. Commented Jul 27, 2010 at 3: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. Commented Jul 27, 2010 at 3: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. Commented Jul 27, 2010 at 3:38
  • @all: The second break was a mistake from copy/paste. Commented Jul 27, 2010 at 3:42

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The second break was a mistake from copy/paste.
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.

2 Comments

The second break was a mistake from copy/paste.
I didn't say anything about that. I knew it was a mistake and ignored it because you had clearly stated your intent.
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.

Comments

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

1 Comment

The second break was a mistake from copy/paste.
-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.