Link to home
Start Free TrialLog in
Avatar of koo9
koo9

asked on

How expensive is TRY-CATCH??

hi all

I just started a new gig and strarting reading someone else's code, I found that in all the functions, there is a try-catch for all the operatings within, now my question is: isn't try catch expensive? or the programmer is just abusing the try catch statement?

I read some article before about how expensive the try catch clasue is, does anyone have any inside on this?

Kevin
Avatar of Molando
Molando

Try-catch slows your code down.
It is better to stop the exceptions happening in the first place, rather than hiding them. Make sure you are not going to divide by zero, make sure that your string is not zero length long etc.

The only times you should use a try-catch is when you are working with something external which you cannot control. E.g. working over a network, where the network connection may suddenly drop. Or loading a dll which could have been replaced.

Molando
Actually... the try in the try block doesn't slow code down one bit, but if it catches an exception, then it gets expensive.

....I think poetic audio is right the only thing that will trigger the the code in the catch block is when an exception occurs. If the code executes the way it should do then it won't even consider the catch block. Having said that moland has a good point in avoiding the exceptions in the first place.
ASKER CERTIFIED SOLUTION
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of koo9

ASKER