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



Question:

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.


Solution:1

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.


Solution:2

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...


Solution:3

Common Exception Types and their Explanations

I think this will probably help you find out what the most appropriate exceptions for you to use are. You can also look into the MSDN documentation for more information on the Exception class and all of its types if you need.


Solution:4

If you check out the MSDN article on System.Exception, right at the bottom of the page is a whole list of BCL exception types that inherit Exception. It's not quite a definitive list on what should be used for what - but it does give you a great place to start checking out exception types. Going through each will tell you what they should be used for:

There is also a fairly comprehensive article regarding the .NET Exception Hierarchy found at:

In fact the whole section in the MSDN library about handling and throwing exceptions is pretty good:


Solution:5

I think the real question is to ask yourself "What type of exception do I want to handle?" If you're not going to have special handling for an InvalidEnumArgumentException or an ArgumentException, then they offer no advantage over a plain old Exception.

I typically either throw Exception or wrap the exception in a custom exception.

Edited to add: At one point, I recall that the guidance from Microsoft was that your application should never throw framework exceptions, rather it should only throw extensions of ApplicationException.


Solution:6

I usually define my own structure of exceptions, first creating some base exception:

class MyProjectNameException:Exception  {  ...constructors, etc  }  

and then derived classes for more precise exceptions. In this way you know exactly when to expect which exception, and you know if the exception was thrown by you or by the framework.


Solution:7

I have most of the common exceptions written on postcards on my desk and use them when people try and interop with me in a way that is exceptional.

This is really good practice for working out which exception fits best...

The one I find myself using mostly is OperationNotSupportedException


Solution:8

If you find an exception that is a good fit, you can use it.

If in doubt, just use ApplicationException. It's the exception that is intended for code that is not part of a standard library.

If the exception is not intended to be used in a standardised way (like catching the base class IOException to catch any I/O related error), the message that you put in the exception is more useful than the type of the exception.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »