Performace of exceptions?
-
Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.
-
Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.
Dr Herbie wrote: What is the performance hit of exceptions, as opposed to returning a null or empty value? Exceptions are VERY expensive, especially the FIRST one (from what I have seen around 600ms). Exceptions are what they are, exceptional conditions (normally from external usage), not some internal bad coding habits. It all depends how your public object/interface will be used. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.
It's recommended that you use as few exceptions as possbile. An exception should never occur in the normal execution of an application. To use exceptions for the program flow is a lazy way of programming. It's slow, and there is a big risk that you accidentally catch any real errors that might occur, and ignore them. --- b { font-weight: normal; }
-
Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.
Another way to look at this is to think in terms of contracts. With the data it's given can a routine fufil its contract, i.e., can it do what it says it can do? If it can then you shouldn't throw an exception. If it can't then it should fail, which means throwing an exception or signalling some other type of failure. Not being able to fulfil its contract is either due to a programmer error, or an environment error such as out of memeory. Either way it will be a rare occurrence. I think in the situation you describe, your gut feeling is probably right. Kevin
-
Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.
To add to what has already been said, it is my experience that any method that may throw an exception should have a way of determining whether that exception will be thrown. To use your dictionary as an example... if the index accessor throws an exception when the key doesnt exist then that is fine.. so long as the Contains method always returns false for that key.
-
Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.
Here's a chapter from a design book on the subject Exceptions[^]. Also, Effective Java and More Effective C++ have good material. I have't seen Effective C#, but I suspect it has a similar take on them and they will all support your argument. I'm not sure the performance angle is the best one to pursue though. I think it is better to point out that it is simply misuse of the feature. The purpose of exceptions is to deal with exceptional conditions. Matt Gerrans