What is ApplicationException for in .NET?
According to the remarks in msdn:
User applications, not the common language runtime, throw custom exceptions derived from the ApplicationException class. The ApplicationException class differentiates between exceptions defined by applications versus exceptions defined by the system.
If you are designing an application that needs to create its own exceptions, you are advised to derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value. For more information, see Best Practices for Handling Exceptions.
Derive them from Exception
. Also, I don't see a problem with creating new exceptions for your cases, as long as it is warranted. If you encounter a case where there is already an exception in the framework, use that, otherwise, roll your own.
Derive from ApplicationException or not?
A whole bunch of exceptions inherit ApplicationException
within native .NET libraries. I have no source of proof, but what I understand is that it's intended to be used for application defined exceptions within .NET libraries.
Event it that wasn't true, the problem you'd be facing when creating custom exceptions for your application that derive from ApplicationException
is that you might be catching a whole bunch of .NET exceptions you don't care or couldn't even handle. Basically, every catch
would need a check to be sure you're catching an exception from your own application rather than .NET.
Take a list of exceptions that derive from ApplicationException
within the documentation page you posted a link to.
Microsoft.JScript.BreakOutOfFinally
Microsoft.JScript.ContinueOutOfFinally
Microsoft.JScript.JScriptException
Microsoft.JScript.NoContextException
Microsoft.JScript.ReturnOutOfFinally
System.Reflection.InvalidFilterCriteriaException
System.Reflection.TargetException
System.Reflection.TargetInvocationException
System.Reflection.TargetParameterCountException
System.Threading.WaitHandleCannotBeOpenedException
And the list is not even complete as there are others: DataSourceSerializationException
, AppConfigException
, NameValidationException
and others...
So whenever one of these are thrown, it would hit your catch (ApplicationException exception)
while you were expecting an exception from your code.
I would strongly suggest continue using your custom base exception.
ApplicationException or create custom exceptions?
Maybe read the documentation:
If you are designing an application that needs to create its own exceptions, you are advised to derive custom exceptions from the
Exception
class. It was originally thought that custom exceptions should derive from theApplicationException
class; however in practice this has not been found to add significant value.
As to whether there are better exceptions to throw - some might consider throwing an ArgumentOutOfRangeException
if you don't want to define your own exception.
Should I derive custom exceptions from Exception or ApplicationException in .NET?
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.
Difference Between Application Exception and System Exception
Originally they were intended to separate BCL defined and user defined Exceptions. ApplicationException was meant to be the base class for all user defined exceptions. The idea was it would give you a clean way to differintiate between framework exceptions and custom exceptions.
Unfortunately this policy was not enforced from the start and as a result there are many contradictions to this rule in the BCL. The current recomendation is not to inherit from these exceptions.
Here's a nice blog entry on the subject:
- http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx
Which built-in .NET exceptions can I throw from my application?
See Creating and Throwing Exceptions.
On throwing built-in exceptions, it says:
Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code.
and
Do Not Throw General Exceptions
If you throw a general exception type, such as Exception or SystemException in a library or framework, it forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle.
Instead, either throw a more derived type that already exists in the framework, or create your own type that derives from Exception."
This blog entry also has some useful guidelines.
Also, FxCop code analysis defines a list of "do not raise exceptions" as described here. It recommends:
The following exception types are too general to provide sufficient information to the user:
- System.Exception
- System.ApplicationException
- System.SystemException
The following exception types are reserved and should be thrown only by the common language runtime:
- System.ExecutionEngineException
- System.IndexOutOfRangeException
- System.NullReferenceException
- System.OutOfMemoryException
So in theory you can raise any other framework exception type, providing you clearly understand the intent of the exception as described by Microsoft (see MSDN documentation).
Note, these are "guidelines" and as some others have said, there is debate around System.IndexOutOfRangeException (ie many developers throw this exception).
ApplicationException' could not be found
The ApplicationException
class isn't available in Portable Class Libraries and ASP.NET vNext projects, which I think your project is.
More importantly:
You should derive custom exceptions from the
Exception
class rather than theApplicationException
class. You should not throw anApplicationException
exception in your code, and you should not catch anApplicationException
exception unless you intend to re-throw the original exception.
So use Exception
when you throw an exception.
Related Topics
JSON Deserialization - Map Array Indices to Properties with JSON.Net
How to Fix the Error:"Unreachable Code Detected"
How to Fill Part of Image with Color
Why Are There System.*.* Dlls in My Bin Release Directory
What Is the Default Value for Enum Variable
Setting Culture for ASP.NET MVC Application on VS Dev Server and Iis
How to Make Form1 Label.Text Change When Checkbox on Form2 Is Checked
Removing the Delay After Keydown Event
Syncing SQL Server 2008 Databases Over Http Using Wcf & Sync Framework
Finding Property Differences Between Two C# Objects
Assert an Exception Using Xunit
The Difference Between Virtual, Override, New and Sealed Override
Overriding Fields or Properties in Subclasses
C# - How to Make Two Forms Reference Each Other
Copying Free Hand Drawing from Panel in Visual Studio 2013
How to Test Private Methods with Nunit
How to Check If a Class Inherits Another Class Without Instantiating It