mardi 29 septembre 2015

The use of ApplicationException

I'd like to know if the use of ApplicationException is recommended in my case. For example, in a service class I want to validate some data before save it in the database. The error messages are returned as a ApplicationException, for example:

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

    if (string.IsNullOrEmpty(email))
        throw new ApplicationException("Type your e-mail");

    if (isValidEmail(email))
        throw new ApplicationException("Invalid e-mail");

    //Save in the database
}

Then I would catch those exception later in a centralized routine. There are a better design than that?

PS: I know that there is better solutions for validating a form, this is just a example and concerns only in the use of ApplicationException to return error messages.

Aucun commentaire:

Enregistrer un commentaire