views:

77

answers:

2

Hi

While using try-catch blocks, even if any exceptions are not thrown, does it consider performance of the code? I wanted to code my own Exception handler which extends from standard exception class, was wondering if this situation downs the performance of the page on several calls.

Regards

+2  A: 

The answer is no, not in any significant way. I suppose over the course of thousands of calls, you might see a few microseconds (or less) difference between some code with a try/catch and some code without, but as try/catch is a language construct, you're not incurring significant overhead.

It's just the same as using a for loop, or an if/else.

The true test is to benchmark your code and see for yourself. ;)

Edit: I should clarify that the actual throwing and catching of an Exception may have some overhead involved, since an Exception object must be generated, etc. This is slightly different than just measuring the try/catch execution itself.

zombat
A: 

Some people I used to work with would always stress how slow exceptions made the code run and that I should minimize use, until I demonstrated that it was the IDE making it slow and not the exceptions themselves. this was visual studio 2003.

of course you should always be mindful of when not to throw an exception. if you are using it as normal flow and a million exceptions are being thrown to affect the control flow then you quite possibly have a bad design anyway.

Simon_Weaver