The use of ApplicationException

asp.net, asp.net-mvc, c#, design-patterns

Solution

From https://msdn.microsoft.com/en-us/library/System.ApplicationException:

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.

One simple reason is that there are other exception classes in .NET derived from `ApplicationException`. If you throw `ApplicationException` in your code and catch it later, you might also catch the derived exceptions which might break your application.

Problem

I'd like to know if the use of `ApplicationException` is recommended to return application errors when a user breaks some business rule. For example: ``` public void validate(string name, string email) { int count1 = (from p in context.clients where (p.name == clients.name) select p).Count(); if (count1 > 0) throw new ApplicationException("Your name already exist in the database"); int count2 = (from p in context.clients where (p.email == clients.email) select p).Count(); if (count2 > 0) throw new ApplicationException("Your e-mail already exist in the database"); } ``` Is it a good or bad strategy? If isn't, what would be a better approach?

Original source

Related problems