Search Unity

FormatException: The specified string is not in the form required for an e-mail address.

Discussion in 'Scripting' started by Nixel2013, Dec 18, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    I have the following error:

    FormatException: The specified string is not in the form required for an e-mail address.
    System.Net.Mail.MailAddress.ParseAddress (System.String address) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    System.Net.Mail.MailAddress..ctor (System.String address, System.String displayName, System.Text.Encoding displayNameEncoding) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    System.Net.Mail.MailAddress..ctor (System.String address, System.String displayName) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    System.Net.Mail.MailAddress..ctor (System.String address) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    Correo+<EnviarDatos>d__6.MoveNext () (at Assets/ComponentesMenu/PanelRegistro/Correo.cs:70)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)


    and I understand why I miss the error.
    but what I don't know is how to make the error "appear" in the form of a debug

    Script line 70:
    Code (csharp):
    1.  mail.From = new MailAddress(CorreoElectronico.text);

    I do not know if there is any way to help me, but the idea is that this error jumps in the form of debug.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I may be misunderstanding what you're asking for here, but I'm guessing that you're taking a user-inputted email address and need to validate that it is a valid address (so you can tell the user to fix it), rather than just having the exception thrown into the Unity console?

    If that's correct, what you're looking for is try-catch:
    Code (csharp):
    1. try {
    2.    mail.From = new MailAddress(CorreoElectronico.text);
    3. }
    4. catch (FormatException e) {
    5.    Debug.Log("This is where you put code to run when the user typed an invalid email");
    6.    Debug.Log("Also, the error message was "+e.Message);
    7. }
    All exceptions can be caught this way. If you have a piece of code that's likely to cause an error (and user input is a big one), you can put try { } catch around it like this to handle error messages gracefully.

    (Note: You should ONLY use try-catch if you actually expect that an exception may occur in normal operation. If you don't expect an exception, then using try-catch there will only hide the cause of the exception from you.)
     
    Nixel2013 and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You should be able to use try/catch to catch the FormatException and then you can do what you want with it, something like so:

    Code (csharp):
    1. try
    2. {
    3.   mail.From = new MailAddress(CorreoElectronico.text);
    4. }
    5. catch( FormatException fe)
    6. {
    7.   // do what you want with the exception referenced by fe here...
    8. }
     
  4. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    Of course, what I want is for you to skip a message that places a valid email so that the user can modify it
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    The other option is just to simply check the email yourself that is in the inputfield. You can use regular expressions so you can give a more meaningful message (missing an @ sign, not period after the @ sign, etc). Grab the text, compare it to a regular expression, if it passes, then continue with the code, otherwise, display your error.
     
  6. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143