c# - The use of ApplicationException -


this question has answer here:

i'd know if use of applicationexception recommended return application errors when user breaks business rule. example:

public void validate(string name, string email) {        int count1 = (from p in context.clients         (p.name == clients.name)         select p).count();      if (count1 > 0)         throw new applicationexception("your name exist in database");      int count2 = (from p in context.clients         (p.email == clients.email)         select p).count();      if (count2 > 0)         throw new applicationexception("your e-mail exist in database");  } 

is or bad strategy? if isn't, better approach?

in code example, better off throwing argumentnullexception more meaningful. applicationexception not give caller indication exception means.

as last check valid email, either argumentexception or custom exception class inherits argument exception best.

 public void update(string name, string email)     {         if (string.isnullorempty(name))         {             throw new argumentnullexception(nameof(name), "type name");         }          if (string.isnullorempty(email))         {             throw new argumentnullexception(nameof(email), "type e-mail");         }          if (isvalidemail(email))         {             throw new argumentexception(nameof(name), "invalid e-mail");         }          //save in database     } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -