Clowo
Clowo

Reputation: 51

Should custom exception classes derive from System.ApplicationException?

https://learn.microsoft.com/en-us/dotnet/api/system.applicationexception?view=net-5.0 says that "ApplicationException Class" Serves as the base class for application-defined exceptions. But in an example at https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions custom exception class derives from "Exception" base class.

Upvotes: 2

Views: 528

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

Well, MSDN states clearly

Important

You should derive custom exceptions from the Exception class rather than the ApplicationException class. You should not throw an ApplicationException exception in your code, and you should not catch an ApplicationException exception unless you intend to re-throw the original exception.

So for custom exception we should use Exception as a base class

Upvotes: 1

GregC
GregC

Reputation: 8007

Fundamentally, it doesn't matter. If your exception type would be a better logical fit when derived from one of the system-provided exceptions, as a user I'd prefer that kind of implementation. Think about what happens when user of your code catches the exceptions, and plan accordingly.

Upvotes: 0

Related Questions