.NET Exceptions – common exceptions and usage guidelines

August 9, 2009 § Leave a comment

My earlier post containing a list of all .NET exceptions gets a lot of traffic. It’s a great reference, but it’s also a really large list and doesn’t give much guidance regarding how to use them. This post will try to give some guidance regarding the more commonly used exceptions and in what circumstances they should be used.

Generally speaking, if you need to throw an exception purposefully, you should probably define a custom exception for that (read the bit about ApplicationException below). There are only a few of the built-in .NET exceptions that you’ll ever want to throw. Here’s my top 5 or so.

System

ApplicationException

The exception that is thrown when a non-fatal application error occurs.
The ApplicationException was originally intended as a base class for application specific exceptions. In other words, you’d derive from ApplicationException rather than Exception for any application specific exception types you needed to define. In practice, most developers don’t seem to do this. Honestly, I don’t know if it’s because they aren’t aware of it, or because it doesn’t really convey any particular benefits.

The obvious benefit of using this class is that if you have multiple application specific exceptions you want to define, and you do derive them all from ApplicationException, you can just catch the base class upstream and handle it. If you’re using a construct like the Microsoft Enterprise Library Exception Handling Application Block, this can come in very handy.

ArgumentException

The exception that is thrown when one of the arguments provided to a method is not valid.
This is a really handy exception that I use all the time. However, you should preferentially use one of the below exceptions. In other words, don’t do something like

throw new ArgumentException("parameter1 is null.");

when you could do something like

throw new ArgumentNullException("parameter1");

ArgumentNullException

The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

ArgumentOutOfRangeException

The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.

NotImplementedException

The exception that is thrown when a requested method or operation is not implemented.
This is a great exception that seems to hardly ever get used. Even some of the stuff that Visual Studio will generate automatically will do something like

throw new Exception("Method is not yet implemented.");

instead of

throw new NotImplementedException();

This obviously isn’t all of them, but it’s a start. The few exceptions I listed above, in combination with custom application-specific exceptions, account for about 90% of the exceptions I ever have to throw.

Advertisements

Tagged: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading .NET Exceptions – common exceptions and usage guidelines at Mike Vallotton's Blog.

meta

%d bloggers like this: