System.Exception and System.ApplicationException

There remains confusion over what to inherit from when creating custom exceptions. Best-practises opinions range from the .NET Framework documentation: “If you are designing an application that needs to create its own exceptions, derive from the ApplicationException class,” through to the .NET Framework Standard Library Annotated Reference (Vol.1): “You should not define new exception classes derived from ApplicationException; use Exception instead. In addition, you should not write code that catches ApplicationException.” So which is it?

System.Exception
If extending the base exception class with additional members, inherit from System.Exception. The name of such inherited classes should end with “Exception.”

Properly, System.Exception should have been declared as abstract with a recommendation to be inherited only by concrete exception classes. Doing so would have avoided the question of which to use right from the start, and would have also helped MS with versioning. However, since things are what they are, in this situation it makes sense to inherit and extend System.Exception. The rule to tend towards a flat hierarchy wins. Inherit from System.Exception when creating a class which adds members.

System.ApplicationException
Applications often provide their own custom exception types (e.g. CmsException, SharePointException) which do not add properties or methods, but simply subclass System.ApplicationException with a new name. If more detailed expections are required for the application, they will then inherit from this base class. This makes it convenient to throw application-specific exceptions that can be identified distinctly inside a try-catch block. When doing the same for your own applications, inherit from System.ApplicationException.

For both System.Exception and System.Application, the rules are consistent in this way: they work with the Framework as it is, not as we would like it to be. Since ApplicationException exists and is in common use, it would be a deviation to derive application-specific exceptions directly from System.Exception. So while you still won't catch ApplicationException directly, you should certainly catch its descendants.

System.SystemException
The difference between ApplicationException and SystemException is that SystemExceptions are thrown by the CLR, and ApplicationExceptions are thrown by Applications. For example, SqlException inherits from SystemException. Included here to make this list complete, there should not be any circumstances where one would need to inherit from SystemException.

These rules are a dump of the model as it exists in my head after a few months of picking up bits and pieces from the various sources. It isn't gospel, but it works for me. Always subject to improvement, feel free to comment.

Further references:
DotNetJunkies article which covers some of this: .NET Anatomy - Structured Exception Handling in .NET
How to Rethrow an Exception Correctly
Design Guidelines Update: Exception Message Guidelines
Exception Management Application Block
Rich Custom Error Handling

Published Monday, May 10, 2004 12:11 PM by erobillard
Filed under: ,

Comments

Monday, May 10, 2004 1:03 PM by Me

# re: System.Exception and System.ApplicationException

> The difference between ApplicationException > and SystemException is that SystemExceptions > are thrown by the CLR, and
> ApplicationExceptions are thrown by
> Applications

Not strictly true, applications can throw SystemException-derived exceptions, e.g. code like the following is common in applications.

throw new ArgumentOutOfRangeException(...);

It is however sensible that applications' custom exceptions shouldn't *inherit* from SystemException.

The grey area is when you are writing a custom class library which is not application-specific and could be considered as an extension to the framework (e.g. a SerialPort class to manage serial I/O through a COM port). In this case it could be argued that you should inherit custom exceptions from SystemException if you can't find an appropriate SystemException-derived exception.

Monday, May 10, 2004 1:09 PM by kpako@yahoo.com (Dare Obasanjo)

# RE: System.Exception and System.ApplicationException

ApplicationException was a very bad idea. Brad Abrams links to some reasons why this is the case at http://blogs.msdn.com/brada/archive/2004/03/25/96251.aspx
Monday, May 10, 2004 2:36 PM by Eli Robillard

# re: System.Exception and System.ApplicationException

Hi Dare,

Correct, that link is actually the source of the quotation in the opening. That page and its feedback say two things to me -- that ApplicationException was a bad idea, and also that it's widely and consistently used.

My own decision to recommend its inheritance when no new members are introduced is a matter of consistency with MS code. And I do believe that including the reference adds value.

One could argue that subclassing System.Exception is needless for any case, that the difference between a DivByZeroException and an NullReferenceException could also be communicated through the Message rather the class name. Why don't we code that way? It's sloppy and doesn't take advantage of the CLR's ability to overload a catch.

Sure, my argument reduces to "because it's there." This is both the weakness and the power of the rationale. Because it's there, and because it is consistently used creates a cultural if not a strictly logical reason to inherit from ApplicationException when no new members are added.

Take care,
Eli.
Monday, October 25, 2004 12:33 AM by TrackBack

# Creating More Exceptional Exceptions

I find myself throwing plain old System.Exception far too often. If only I had a complete reference of the many default Exception classes Microsoft provides, like the one Chris Sully provides in his article. That'sgood as a starting point,...

Leave a Comment

(required) 
(required) 
(optional)
(required)