- Home /
Why is the try catch method in Unity not preventing my app from quitting/crashing?
I have two methods SaveToiCloud
and GetFromiCloud
that are doing what they say. I removed on purpose any null checks to see if the try and catch could do their job alone. However in Unity it is not crashing but when I build it to swift it is. "terminating with uncaught exception of type NSException (lldb) " . I thought I'am catching every exception... Also the crash ONLY happens when I first try to get the value (this is not giving me an error!) and then try to save it (this is). I think this is because I'am setting the value to null with GetFromiCloud
, but then again I thought the exception should be catched.
public void SaveToiCloud()
{
try{
// all variables are stored on GlobalD. with default value assigned to them
// all variables in this class are stored with no default value (to check)
level = GlobalData.instance.level;
health = GlobalData.instance.health;
doorOpen = GlobalData.instance.doorOpen;
nameplayer = GlobalData.instance.nameplayer;
IntToDictionary(); //I have made sure no keys are overriding
StringToDictionary();
BoolToDictionary();
iCloudSaveValue();
}
catch (System.Exception ex)
{//Unity is successfully showing me this exception without crashing/quitting
Debug.Log(ex);
Debug.Log(ex.Message);
Debug.Log(ex.StackTrace);
}
}
public void GetFromiCloud()
{
try
{
GetIntFromiCloudDic();
GetStringFromiCloudDic();
GetBoolFromiCloudDic();
GlobalData.instance.level = level;
GlobalData.instance.health = health;
GlobalData.instance.doorOpen = doorOpen;
GlobalData.instance.namePlayer = namePlayer;
SaveSystem4.SavingToFile();
}
catch (System.Exception ex)
{
Debug.Log(ex);
Debug.Log(ex.Message);
Debug.Log(ex.StackTrace);
}
public void iCloudSaveValue()
{
foreach (KeyValuePair<string, int> pair in DicInt)
{
bool success = iOSPlugin.iCloudSaveIntValue(pair.Key, pair.Value);
if (success)
{
iOSPlugin.ShowAlert("iCloud Value Saved Success", pair.Value.ToString());
}
else
{
iOSPlugin.ShowAlert("iCloud Value Saved failed", pair.Value.ToString());
}
}
void GetIntFromiCloudDic()
{
try
{
level = iOSPlugin.iCloudGetIntValue("Level"); // I know I should use null check
health = iOSPlugin.iCloudGetIntValue("Health");
}
catch
{
iOSPlugin.ShowAlert("iCloud Value", string.IsNullOrEmpty(nameplayer) ? "Nothing Saved Yet..." : level.ToString());
iOSPlugin.ShowAlert("iCloud Value", string.IsNullOrEmpty(nameplayer) ? "Nothing Saved Yet..." : health.ToString());
}
}
Answer by Pangamini · Sep 28, 2019 at 09:09 PM
Your c# code cannot catch exceptions produced eg. by c++ runtime, as the two are not the same thing. You have to catch it on the native code side, and optionally pass it to the Unity as a value, though I haven't studied this problem much and there might be a better way.
Your answer
Follow this Question
Related Questions
Stop Game on Exception 4 Answers
C# try catch block doesn't work for iOS? 2 Answers
Try/Catch block stops exception from happening 0 Answers
Try Catch not working with VideoPlayer 2 Answers