C# try catch block doesn't work for iOS?

Trying to build an app to iOS, & to my surprise it seems code in a standard C# try-catch block hangs the build. Does try-catch work in an iOS build? I hope I can post code here properly (first time), but the offending code is:

try {
    		xmlDoc.LoadXml(xmlDocString);
    		try {
    			steering = float.Parse(xmlDoc.DocumentElement.Attributes["steering"].Value);
    		} catch {
    			steering = 0f;
    		}
} catch {
}

It hangs on the inner try catch block. I nested the try-catch blocks to isolate the problem. xmlDoc is an XmlDocument object that loads an XML string sent via a SendMessage (so the string is cast from type object). I understand an exception may occur because the XML string may not contain the attribute. But isn’t try-catch supposed to handle this kind of exception handling? Does Android build have the same issue?

Any info welcome & appreciated. Thanks.

Just curious, did you ever get this to work? I'm on iOS too, getting the same error with try { System.Convert.FromBase64CharArray(charArray, 0, charArray.Length); } catch //Also tried catch (System.FormatException) since that's the error it's throwing but no luck { }

Unfortunately I can't test on iOS, and I know the clue of the problem is try catch, but as a side note, you can convert the inner one to: float steering = 0f; var attrSteering = xmlDoc.DocumentElement.Attributes["steering"]; if(attrSteering == null || !Single.TryParse(attrSteering.Value, out steering)) { steering = 0f; }

I've also had this problem on iOS, and removing the try catch block fixed the error (and I did have Slow and Safe selected).

2 Answers

2

Do you have exception handling enabled? If you want to catch exceptions on iOS you should make sure that the exception handling has been enabled (from Project Settings → Player Settings → iOS → Other Settings set “Script Call Optimization” to “Slow and Safe”).

It should work. But you can’t really figure out the problem if you don’t know what it is. Change your catch methods to this

catch(Exception e){
     Debug.log(e.toString());
}

That will help you isolate the problem, it’s probably something really simple but you won’t know unto you see what the errors are

Error will print in xcode not unity fyi