1

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Clowo
  • 41
  • 3
  • https://stackoverflow.com/questions/20391287/creating-my-own-exceptions-c-sharp/20392944#20392944 – Dmitry Bychenko Jun 26 '21 at 12:04
  • 1
    https://learn.microsoft.com/en-us/dotnet/api/system.applicationexception?view=net-5.0 "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." – Dmitry Bychenko Jun 26 '21 at 12:06

2 Answers2

1

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

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • When i read i took application-defined exception and custom defined exception as same things. So what is application-defined exception. – Clowo Jun 26 '21 at 12:26
  • 1
    Clowo: "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." https://stackoverflow.com/questions/5685923/what-is-applicationexception-for-in-net – Dmitry Bychenko Jun 26 '21 at 12:35
0

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.

GregC
  • 7,737
  • 2
  • 53
  • 67