Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I chose the most appropriate type of exception to throw?

Tags:

There are already lots of questions on SO about exceptions, but I can't find one that answers my question. Feel free to point me in the direction of another question if I've missed it.

My question is quite simple: how do other (C#) developers go about choosing the most appropriate type of exception to throw? Earlier I wrote the following code:

    if (Enum.IsDefined(enumType, value))     {         return (T)Enum.Parse(enumType, value);     }     else     {         throw new ArgumentException(string.Format("Parameter for value \"{0}\" is not defined in {1}", value, enumType));     } 

I have since realised that throwing an InvalidEnumArgumentException would probably have been more appropriate had I known of its existence at the time.

Is there an authoritative resource available that helps developers chose exception types, or is it simply a matter of experience?

Edit

I've given the points to Noldorin for providing a range of ideas in a well thought-out answer. The points could have gone to any one of you really - thanks for all the suggestions.

like image 572
Paul Suart Avatar asked May 27 '09 12:05

Paul Suart


People also ask

What Java exception should I throw?

Only checked exceptions are required to be thrown using the throws keyword. Unchecked exceptions don't need to be thrown or handled explicitly in code.

Is it a good approach to throw an exception?

The short answer is NO. You would throw an exception if the application can't continue executing with the bad data. In your example, the logic is to display an error message on the front end and Option 2 is the cleaner method for achieving this requirement.

When should we throw an exception?

As a general rule of thumb, exception usage should balance the following two opposing goals: Exceptions should not be the norm. They involve the creation of an additional object, so, if only from a performance standpoint, it is problematic if exceptions can occur frequently. Mixing data and control should be avoided.


2 Answers

Krzysztof Cwalina has a good post on this see chapter "1.1.1 Choosing the Right Type of Exception to Throw"

PS Consider subscribing to his blog. Good reading!

To answer your question: InvalidEnumArgumentException
because throw the most specific (the most derived) exception that makes sense.

AND callers that catch ArgumentException, catch InvalidEnumArgumentException too...

like image 109
Peter Gfader Avatar answered Oct 16 '22 15:10

Peter Gfader


I would say it's just down to experience. There's still new exceptions I discover every so often, and I've been working with many aspects of .NET for a while now! What would you want this source to tell you, anyway? Choosing the appropriate exception type would seem highly context-specific, so I'm doubtful over the level of advice it could offer. Listing the more common ones would be most it could provide. The names and Intellisense descriptions of the exception types typically explain with good clarity their usage scenarios.

My recommendation is simply to familiarize yourself with all of the fundamental ones (specifically, those in System, System.IO, and any other namespaces you often use), and learn the others along the way. I find that I generally get away using just a small number. If you accidentally use a more generic exception type when there already exists a more specific one in the BCL, then it's no great crime, and can be changed later easily enough. To be honest, for any error that's particularly specific, you will often need to create your own class inheriting from Exception anyway.

Hope that helps.

Edit: If you want a brief guide to the very common ones, see the Common Exception Classes page on MSDN.

like image 31
Noldorin Avatar answered Oct 16 '22 14:10

Noldorin