?
Solved

How expensive is TRY-CATCH??

Posted on 2005-03-03
5
Medium Priority
?
1,299 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:koo9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:Molando
ID: 13451486
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
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 13451716
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
 

Expert Comment

by:ramrod1979
ID: 13452415
....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
 
LVL 17

Accepted Solution

by:
Jesse Houwing earned 100 total points
ID: 13452878
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question