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

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+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:

 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.

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:

try {
   mail.From = new MailAddress(CorreoElectronico.text);
}
catch (FormatException e) {
   Debug.Log("This is where you put code to run when the user typed an invalid email");
   Debug.Log("Also, the error message was "+e.Message);
}

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.)

2 Likes

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:

try
{
  mail.From = new MailAddress(CorreoElectronico.text);
}
catch( FormatException fe)
{
  // do what you want with the exception referenced by fe here...
}

Of course, what I want is for you to skip a message that places a valid email so that the user can modify it

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.