4

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?

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
Fabio Belz
  • 258
  • 2
  • 6
  • 12
  • There's not much value to throwing ApplicationException over System.Exception http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx – Eric J. Sep 29 '15 at 21:48
  • 4
    For this type of thing it's common to use `ArgumentException`, since you're validating method arguments. – Dave Zych Sep 29 '15 at 21:53

3 Answers3

9

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.

riQQ
  • 9,878
  • 7
  • 49
  • 66
Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
8

In your code example, you would be better off throwing an ArgumentNullException it is more meaningful. ApplicationException does not really give the caller any indication as to what the exception means.

As for the last check for the valid email, either an ArgumentException or a custom exception class that inherits from Argument exception would be best.

 public void update(string name, string email)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException(nameof(name), "Type your name");
        }

        if (string.IsNullOrEmpty(email))
        {
            throw new ArgumentNullException(nameof(email), "Type your e-mail");
        }

        if (isValidEmail(email))
        {
            throw new ArgumentException(nameof(name), "Invalid e-mail");
        }

        //Save in the database
    }
Richard Seal
  • 4,248
  • 14
  • 29
4

You should actually use Exception to create custom exception in your application by deriving from it. Though you should also read this answer.

For you specific sample case argument validations should throw ArgumentException if it wouldn't have existed then you could have created a custom class derived from Exception class for this .

vendettamit
  • 14,315
  • 2
  • 32
  • 54