5

Exception handling on Windows boxes (at least for C++) takes a performance hit if you exit a try block prematurely (such as executing a return statement) the same as if an exception were thrown.

But what about C#? Is there a performance hit for returning prematuraly from a try block, whether through a return statement or break statement?

4
  • 4
    "Exception handling on Windows boxes (at least for C++) takes a performance hit if you exit a try block prematurely...the same as if an exception were thrown." Um...really? That's very surprising. Data? Commented May 12, 2010 at 5:47
  • Closely related (at least): stackoverflow.com/questions/867017/performance-cost-of-try Commented May 12, 2010 at 5:48
  • The perf hit in C++, for exiting prematurely, is covered in Jeffrey Richter's "Windows via C/C++". Commented May 12, 2010 at 6:03
  • What's the actual quote? Because I cannot see any difference between return-ing out of a try block and exiting it "normally". The costs in exception structures are typically at entry and exit (regardless of how), which both have very, very small costs in modern systems, and actually throwing an exception, which can be quite costly indeed. (But that's okay, it is -- by definition! -- an exceptional condition.) Commented May 12, 2010 at 6:10

3 Answers 3

7

If there is a performance hit, it's tiny. It's certainly nothing like the same as catching the exception. (And even that's not as bad as many people think.)

As far as I'm aware, the performance of returning from a try block is negligible. The chance of it being significant in your app is essentially 0. Just write the most readable code you can, and then benchmark/profile your app - that will be a much better way to get good performance than trying to second guess this sort of thing.

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

4 Comments

+1 "the performance of returning from a try block is negligible" And I can't imagine it's any different at all if you issue a return vs. just dropping out of the block.
+1 "Premature optimization is the root of all evil" Donald Knut
Entering an actual try block itself is much costlier. I can't really imagine there is any difference between returning inside a try or returning after the try.
Would this still count if the exception was wrapped in a "finally" as this is similar to catching a thrown exception?
3

No, there is no penalty what so ever for exiting prematurely from a try block.

I timed the difference between calling a method that returns in the middle of a try block and one that exits naturally out of the try block before returning, and there is no measurable difference.

There is a slight overhead for having the try block (about 0.000002 ms.), but no extra overhead for exiting in the middle of it.

Comments

-1

All exception handling incurs a small performance overhead - there is always additional work that has to be done for any try/catch in existence.

THe question is not whether it is there, but whether it is RELEVANT. If I need to catch an exception, i need to. No dispute about performance.

1 Comment

That doesn't answer his question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.