eBabel Blog

Make a website

System.Exception vs ApplicationException

15 March 2010

System.SystemException

SystemException, like ApplicationException, is also part of the .Net classes.

Namespace: System
Assembly: Mscorlib (in Mscorlib.dll)

The SystemException class is the counter part to the ApplicationException:

This class is provided as a means to differentiate between exceptions defined by the system versus exceptions defined by applications. SystemException does not provide information as to the cause of the Exception. In most scenarios, instances of this class should not be thrown. In cases where this class is instantiated, a human-readable message describing the error should be passed to the constructor. SystemException is thrown by the common language runtime when errors occur that are nonfatal and recoverable by user programs. These errors result from failed runtime check (such as an array out-of-bound error), and can occur during the execution of any method. SystemException adds no new functionality to Exception.

Source: Microsoft

Inspecting the .NET object hierarchy System.SystemException is under System.Exception

System.Object
	System.Exception
		System.SystemException

System.ApplicationException

ApplicationException is a core .NET class

Namespace: System
Assembly: Mscorlib (in Mscorlib.dll)

In your application code if is useful to use the ApplicationException class (in stead of the higher-level System.Exception) as:

ApplicationException is thrown by a user program, not by the common language runtime. If you are designing an application that needs to create its own exceptions, derive from the ApplicationException class. ApplicationException extends Exception, but does not add new functionality. This exception is provided as means to differentiate between exceptions defined by applications versus exceptions defined by the system. ApplicationException does not provide information as to the cause of the exception. In most scenarios, instances of this class should not be thrown. In cases where this class is instantiated, a human-readable message describing the error should be passed to the constructor.

Source: Microsoft

Inspecting the .NET object hierarchy System.ApplicationException is under System.Exception

System.Object
	System.Exception
		System.ApplicationException

Why use one type of exception or the other?

I was trying to figure this question out and fortunately there already appears to be a definitive answer on stackoverflow:

Q: What is best practice when creating your exception classes in a .NET solution: To derive from System.Exception or from System.ApplicationException? A: According to Jeffery Richter in the Framework Design Guidelines book: System.ApplicationException is a class that should not be part of the .NET framework. It was intended to have some meaning in that you could potentially catch "all" the application exceptions, but the pattern was not followed and so it has no value.

Source: Stackoverflow

For the 70-536 exam, you need to be aware of the System.Exception and System.ApplicationException and that in general it is best to derive from the System.Exception:

A SystemException is usually reserved for the .NET runtime/framework to use, and not your application code. Basically, don't derive from SystemException when creating your own custom Exception class. If you are creating your own Exception classes, you should either derive them from Exception or ApplicationException. ApplicationException was originally intended to be used for non-framework exceptions, but it has sort of fallen to the wayside. I believe the framework authors now recommend to derive your custom Exceptions from the base Exception class.

Source: Stackoverflow

If you liked this blog post, you may want to subscribe to my news feed in your RSS reader.