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?

Brent Arias
  • 29,277
  • 40
  • 133
  • 234
  • 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? – T.J. Crowder May 12 '10 at 05:47
  • Closely related (at least): http://stackoverflow.com/questions/867017/performance-cost-of-try – T.J. Crowder May 12 '10 at 05:48
  • The perf hit in C++, for exiting prematurely, is covered in Jeffrey Richter's "Windows via C/C++". – Brent Arias May 12 '10 at 06: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.) – T.J. Crowder May 12 '10 at 06:10

3 Answers3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    +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. – T.J. Crowder May 12 '10 at 05:59
  • +1 "Premature optimization is the root of all evil" Donald Knut – bartek May 12 '10 at 06:17
  • 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. – Steven May 12 '10 at 06:28
  • Would this still count if the exception was wrapped in a "finally" as this is similar to catching a thrown exception? – Mongus Pong May 12 '10 at 10:20
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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
-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.

TomTom
  • 61,059
  • 10
  • 88
  • 148