mustafabar
mustafabar

Reputation: 2359

which is a better practice at exception handling?

if I have a specific exception that I expect when it is going to occur; and to handle it for example I chose to display an error message upon its occurance, which would be better to do, and why?

Explanatory code:

try
{
    string result = dictionary[key];
}
catch (KeyNotFoundException e) 
{ 
    //display error
}

or:

if(!dictionary.ContainsKey(key))
{
    //display error
}

Upvotes: 2

Views: 530

Answers (10)

RossFabricant
RossFabricant

Reputation: 12492

The second approach is better for at least 3 reasons:

1) It is clearer. As a reader of your code, I expect an exception to indicate that something has gone wrong, even if it's handled.

2) When debugging with Visual Studio it's common to break on all exceptions, that makes it mildly annoying to deal with code which always throws an exception.

3) The second version is faster, but the effect is very small unless you are throwing many exceptions a second in a time-critical piece of code.

Upvotes: 2

thecoop
thecoop

Reputation: 46098

Generally, exceptions are used to indicate exceptional conditions - something that would not normally occur, but your program still needs to handle gracefully (eg a file being inaccessible or readonly, a network connection going down). Normal control flow, like checking for a value in a dictionary, should not use exceptions if there is an equivalent function that has the same effect without using exceptions.

Having extra try/catch statements in the code also makes it less readable, and having exception handlers around a block of code puts certain limitations on the CLR that can cause worse performance.

In your example, if it is expected that the dictionary will have a certain key value, I would do something like

string result;
if (!dictionary.TryGetValue(key, out result)
{
    // display error
    return;   // or throw a specific exception if it really is a fatal error
}

// continue normal processing

This is a lot clearer than just having an exception handler round an element access

Upvotes: 12

D.Shawley
D.Shawley

Reputation: 59553

The second approach is probably better. Remember that exceptions are used for exceptional circumstances. Use this principle to guide your decision:

  • If you require that the key exists in the dictionary as an application invariant, then assume that it is there and deal with the exception if it isn't there.
  • If your application code doesn't require that the entry exist in the dictionary, then call ContainsKey() first.

My guess is that the latter is probably the correct course of action.

Disclaimer: I generally eschew the advice that performance should be the primary consideration here. Only let performance impact your decision once you have proven that you have a bottleneck! Anything before that is premature optimization and will lead to unnecessarily complication application code.

Upvotes: 2

Steve
Steve

Reputation: 12004

It depends greatly on what the dictionary is meant to do. If there is a high chance that the key will not be found because of program design, then you should do the trygetvalue. However, if the design of your program is such that not finding the key is an exceptional event, you should use the exception handler method.

Upvotes: 0

Test
Test

Reputation: 1727

Just a reminder. It is not an exception. An exception is something sounds like "no /etc on my UNIX machine". If you get wrong sense, you'll write wrong code shown as above.

Upvotes: -2

Guffa
Guffa

Reputation: 700152

Neither.

The second option is better than the first. As you expect this to happen normally, it's better to avoid the exception. Exceptions should preferrably only be used for exceptional situations, i.e. something that you can't easily predict and test for.

The best option however is the TryGetValue method, as it does both check and fetch:

if (dictionary.TryGetValue(key, out result)) {
   // use the result
} else {
   // display error
}

Upvotes: 7

ChrisF
ChrisF

Reputation: 137108

It all depends on what you're application is doing and what the specific code is doing.

As thecoop says exceptions should be used for exceptional conditions. As an illustration take writing to a file. If checking for the existence of the file would slow your application down and/or the absence of the file is a serious problem then allow the exception to occur and trap that. If it's not so critical or recreating the file isn't a problem then do the file existence check first.

I've seen it argued that all error handling should be done via exceptions (most recently in Clean Code by Robert Martin) but I don't agree.

Upvotes: 0

Artelius
Artelius

Reputation: 49078

Exception handling is most useful when you need to provide an easy way out of a difficult situation - it can greatly simplify the code and decrease the potential for corner-case bugs.

It offers little advantage in very simple situations like this, and due to its performance penalty should not be used in such cases.

Upvotes: 1

Dzmitry Huba
Dzmitry Huba

Reputation: 4521

The second approach is better because throwing and hanlding exception has its performance hit. Throw rates above 100 per second are likely to noticeably impact the perfor- mance of most applications. Consider Exceptions and Performance.

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166326

The second approuch is better. Exception throwing can be very expensive.

Upvotes: 3

Related Questions