void f1()
{
try
{
f2();
}
catch { }
}
void f2()
{
// This func fails and throws an error.
}
Will the error in f2 be watched in the catch if I called f1()?
void f1()
{
try
{
f2();
}
catch { }
}
void f2()
{
// This func fails and throws an error.
}
Will the error in f2 be watched in the catch if I called f1()?
yes.
why don’t you try it (no pun intended)? you can throw errors yourself.
void f2() {
throw new System.ArgumentException();
}
don’t forget to catch it fully qualified
i.e.
try {
...
} catch(Exception ex) {
Debug.Log(ex.message);
}
Though to be honest, catching is rarely used in Unity. The whole thing is rather slow for game dev.
I mostly get away from it by using the appropriate try… methods (not that they’re intended as a substitute, but that’s how this turns out most of the time)
For example if you want to parse numbers, you use TryParse instead. If you want to get a key from a dictionary, you use TryGetValue instead. I only ever throw errors when I validate arguments (I’m sure there are other cases as well but can’t think of any right now). And even that I use almost exclusively in edit mode, I enclose most of these with a preprocessor clause
#if UNITY_EDITOR || DEBUG
if(something is null) throw new ArgumentNullException(nameof(something));
#endif
With math, you never have to catch anything because division by zero produces NaNs.
You only truly use catch when working with outside environments, DLLs, data streams, disk I/O, web transaction, that sort of thing. Or ok, maybe if you have to rely on 3rd party assets that throw something under certain circumstances, that’s legit, I guess.