James
James

Reputation: 1663

Handling exceptions in Unity

I am working with C# and Unity and putting together a library that I can use in Unity and elsewhere. This makes sense to me as it is for networking and Id like the data handlers and such to generally be the same. For compatibility between Unity and other more up to date C# environments I am putting in extensions where I can like the following:

public static class PropertyInfoExtension
{
    public void SetValue(this PropertyInfo propertyInfo, object obj, object value)
    {
        propertyInfo.SetValue(obj, value);
    }
}

The error, which was not obvious to me, is that this is calling itself in Unity's version of C#. This code was run on its own thread and this is where I think I am doing something wrong as this should have caused a buffer overflow exception.

I have a registered AppDomain.CurrentDomain.UnhandledException.. I have put in a logging system that wraps to the UnityEngine.Debug.Log API. I put in try {} catch {} blocks but still Unity would just crash half the time and hang the other half of the time.. I would randomly get an error message and the first time I got the dump from the UnhandledException handler it put me on the track to narrow this down but it feels like I am using C# in Unity improperly but I can not find out what I am supposed to be doing.

EDIT: I guess in one of my revamps of this question I removed explicitly what it is.. What is the right way to get errors out of Unity? Why did this error cause Unity to crash instead of the app to crash and throw its exception? why did the UnhandledExcpetion handler only get called once out of 2 hours of testing?

Those all sum up to how am I supposed to reliably develop a robust error handling system in Unity because apparently what I would normally do in C# is not good enough.

Upvotes: 0

Views: 2236

Answers (2)

CodingYoshi
CodingYoshi

Reputation: 27049

In the comments to you question you asked this:

I am just looking at how to do reliable error handling in Unity.. because the editor crashing when the app has a stack overflow instead of throwing an exception

Unfortunately you cannot handle StackOverflowException as of .NET 2.0. Here is quote from MSDN

In the .NET Framework 1.0 and 1.1, you could catch a StackOverflowException object (for example, to recover from unbounded recursion). Starting with the .NET Framework 2.0, you can’t catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.

All you can do is to fix your code. Make sure if you are using recursion, they have a terminating condition. Make sure your setters (in properties) are not calling the getters. That's all the help I can offer.

Upvotes: 3

Matt
Matt

Reputation: 1484

It seems to me that you are having StackOverFlow because you are calling the same method again inside the extension method. So with the following:

public static class PropertyInfoExtension
{
    public void SetValue(this PropertyInfo propertyInfo, object obj, object value)
    {
        propertyInfo.SetValue(obj, value);
    }
}

you are essentially doing this:

public static class PropertyInfoExtension
{
    public void SetValue(this PropertyInfo propertyInfo, object obj, object value)
    {
        propertyInfo.SetValue(obj, value)
        {
            // public void SetValue(this PropertyInfo propertyInfo, object obj, object value)
            propertyInfo.SetValue(obj, value)
            {
                propertyInfo.SetValue(obj, value)
                {
                    ...
                }
            }
        }
    }
}

Think about recursive methods that call the same method without terminating point.

public int InfiniteRecursiveCausesStackOverFlow(int a)
{
    return InfiniteRecursiveCausesStackOverFlow(a);
}

Each method call is recorded in the Stack, and infinite method loops like this would make your Stack get more memory required than what it can handle, hence called the StackOverFlow exception.

To resolve this issue you would need to call the actual business logic inside the method.

Hope this helps.

Upvotes: 0

Related Questions