0

I'm using this code to validate a email address that the user types in a text box. The code works but the only problem is if the user types a valid email than delets it I get an error saying that the address parameter can't be null. It is not going to the catch part on an error. Why?

   private bool checkMailLL(string mail)
    {
        try
        {
            var test = new MailAddress(mail);
            return true; //valid email
        }
        catch (FormatException ex)
        {
            return false; //invalid email
        }
    }
Aman
  • 582
  • 2
  • 12
  • 29
  • I understant that I can check if it is empty first. What I want to know is why it is giving me an error istead of throwing a catch exception – Aman Oct 23 '14 at 12:44
  • Because its not a format-exception. If mail is null, there is nothing to be checked for the correct format. You could change "FormatException" to "Exception" and then go => "MessageBox.Show(ex.ToString());" to check the output and the type of exception. – C4d Oct 23 '14 at 12:45
  • @Aman: simply because you are catching only one possible exception but not that one that is thrown if the string is empty (or null). Have a lok at MSDN where it's documented. I've already added the link to the documentation in [my answer](http://stackoverflow.com/a/26528394/284240). BTW , sorry that i've deleted that comment because i've posted an answer and i've overlooked that you've already commented it. – Tim Schmelter Oct 23 '14 at 12:49
  • Thanks I didn't know that I could use only Exception. – Aman Oct 23 '14 at 12:51
  • @Aman: yes, you can handle _any_ exception by catching `Exception`. But in general that's not best-practices since you could mistakely swallow exceptions which should not be handled. – Tim Schmelter Oct 23 '14 at 12:53
  • @Aman: As Tim Schmelter said, dont start using it everywhere. But to check WHICH exception is thrown, you can temporarily set it to "Exception" only and then output the error in a messagebox. In the first line of the MessageBox, there will be the type of exception. – C4d Oct 23 '14 at 12:56

3 Answers3

3

The constructor throws an ArgumentException in the case that the address is empty, so you also have to catch that:

private bool checkMailLL(string mail)
{
    try
    {
        var test = new MailAddress(mail);
        return true; //valid email
    }
    catch (FormatException)
    {
        return false; //invalid email
    }
    catch (ArgumentException)
    {
        return false; //invalid email
    }
}

or a little bit simpler, check it first manually which handles also the possible ArgumentNullException:

private bool checkMailLL(string mail)
{
    if(string.IsNullOrWhiteSpace(mail)) return false;
    try
    {
        var test = new MailAddress(mail);
        return true; //valid email
    }
    catch (FormatException )
    {
        return false; //invalid email
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

Check if mail is null first:

private bool checkMailLL(string mail)
{
    if (string.IsNullOrEmpty(mail))
       return false;    

    try
    {
        var test = new MailAddress(mail);
        return true; //valid email
    }
    catch (FormatException ex)
    {
        return false; //invalid email
    }
}

The MailAddress constructor wont allow you to pass in null, and it throws a different exception than FormatException which is why the exception isn't caught.

Also, you might want to take a look at this for a better way for validating Emails: Best Regular Expression for Email Validation in C#

Community
  • 1
  • 1
  • Using string.IsNullOrEmpty(mail) gives me the same error I used mail != "" and it worked – Aman Oct 23 '14 at 12:46
  • I was using a regular expression to validate my emails but I was told that using regular expression is not recomended so thats why I changed to the MailAdress(mail). In this case would it be better to use regular expression? – Aman Oct 23 '14 at 12:59
  • Generally it's considered bad practice to use a try/catch block to control program flow as it's computationally expensive to reach the catch block. So if there's an alternative way to validate the string `mail` that doesn't involve throwing an exception, in principle, it's better. Additionally, RegEx comparison, especially if it's precompiled, is very fast, likely faster than new-ing up a new object. So yes, I would say you'd be better using a Regex. This MSDN article will explain more: http://msdn.microsoft.com/en-us/library/01escwtf%28v=vs.110%29.aspx –  Oct 23 '14 at 17:43
1
private bool checkMailLL(string mail)
    {
        if (String.IsNullOrEmpty(mail)) return false;
        try
        {
            var test = new MailAddress(mail);
            return true; //valid email
        }
        catch (FormatException ex)
        {
            return false; //invalid email
        }
    }
David Pilkington
  • 13,528
  • 3
  • 41
  • 73