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
koo9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MolandoCommented:
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
0
PoeticAudioCommented:
Actually... the try in the try block doesn't slow code down one bit, but if it catches an exception, then it gets expensive.

0
ramrod1979Commented:
....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.
0
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
All advice given is correct, I just thought I'd add a little extra
A snippet from Apress-Maximizing .Net performance on the subject of exceptions and performace:

Coding to Avoid Exceptions
Raising and catching exceptions should not routinely occur as part of the successful execution of a method. When developing class libraries, client code must be given the opportunity to test for an error condition before undertaking an operation that can result in an exception being raised. For example, System.IO.FileStream provides a CanRead property that can be checked prior to calling the Read method, preventing a potential exception being raised, as illustrated in the following code snippet:

Dim str As Stream = GetStream()
If (str.CanRead) Then
  'code to read stream
End If

The decision of whether to check the state of an object prior to invoking a particular method that may raise an exception depends on the expected state of the object. If a FileStream object is created using a file path that should exist and a constructor that should return a file in read mode, checking the CanRead property is not necessary; the inability to read the FileStream would be a violation of the expected behavior of the method calls made, and an exception should be raised. In contrast, if a method is documented as returning a FileStream reference that may or may not be readable, checking the CanRead property before attempting to read data is advisable.

To illustrate the performance impact that using a "run until exception" coding technique can cause, the performance of a cast, which throws an InvalidCastException if the cast fails, is compared to the C# as operator, which returns nulls if a cast fails. The performance of the two techniques is identical for the case where the cast is valid (see Test 8.05), but for the case where the cast is invalid, and using a cast causes an exception, using a cast is 600 times slower than using the as operator (see Test 8.06). The high-performance impact of the exception-throwing technique includes the cost of allocating, throwing, and catching the exception and the cost of subsequent garbage collection of the exception object, which means the instantaneous impact of throwing an exception is not this high. As more exceptions are thrown, frequent garbage collection becomes an issue, so the overall impact of the frequent use of an exception- throwing coding technique will be similar to Test 8.05.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.