callback for unhandled exceptions?

Unhandled exceptions get logged in that magical web player log, but I'd also like to capture them and send them back to the server (so we know where our game is having problems). I noticed Application.LogCallback, and set up a delegate. The delegate captures other problems (like failed downloaded files), but when I threw an exception on purpose to test, it didn't get captured.

Is there a different callback I should use to capture these kinds of events, that get output to the unity log but aren't reported to LogCallback? Alternatively, is htere a way to get the entire contents of the log file from within unity?

--- edit Actually, it doesn't start working until the frame after the callback is registered... duhh

As Denise found, Application.LogCallback does work, but it isn’t actually hooked up and functional until the frame after you make the call.

Here is a script that demonstrates exception logging. Attach this script to a game object in your scene and it will give you four on-screen buttons that generate different error conditions.
(exception handler screenshot)

Running this reveals a few interesting things:

  1. Application.LogCallback does work, and will be called when exceptions occur.
  2. Perhaps surprisingly, floating point divide by zero does not throw an exception. This is part of the .NET floating point specification, and it makes sense, since floating point numbers have a representation for infinity. Integer divide by zero does throw an exception, as there is no way to represent infinity with an integer.
  3. While Unity will occasionally catch a stack overflow exception, you can’t rely on this – usually the Unity editor just dies.

Note that the script generates a log file in your Assets folder. You may need to reimport this asset after running in order to see the latest changes from in the Unity editor.

using System;
using System.IO;
using UnityEngine;

public class ExceptionHandler : MonoBehaviour
{
    private StreamWriter m_Writer;
    private int m_ExceptionCount = 0;
    
    void Awake()
    {
        Application.RegisterLogCallback(HandleException);
        m_Writer = new StreamWriter(Path.Combine(Application.dataPath, "unityexceptions.txt"));
        m_Writer.AutoFlush = true;
    }
    
    private void HandleException(string condition, string stackTrace, LogType type)
    {
        if (type == LogType.Exception)
        {
            m_ExceptionCount++;
            m_Writer.WriteLine("{0}: {1}

{2}", type, condition, stackTrace);
}
}

    void OnGUI()
    {
        GUILayout.Label(string.Format("Count: {0}", m_ExceptionCount));
        if (GUILayout.Button("Application Exception"))
        {
            throw new ApplicationException();
        }
        if (GUILayout.Button("Null Reference"))
        {
            GameObject go = null;
            Debug.Log(go.name);
        }
        if (GUILayout.Button("Float Divide By Zero"))
        {
            float x = 3.14f;
            float y = 0.0f;
            float z = x / y;
            Debug.Log(z.ToString());
        }
        if (GUILayout.Button("Integer Divide By Zero"))
        {
            int x = 42;
            int y = 0;
            int z = x / y;
            Debug.Log(z.ToString());
        }
        if (GUILayout.Button("Stack Overflow"))
        {
            OverflowStack(1, 2, 3);
        }
    }
    
    private int OverflowStack(int a, int b, int c)
    {
        return OverflowStack(c, b, a) + OverflowStack(b, c, a+1);
    }
}

Here is my version based on YoYo’s answer. I use it to call my own MessageBox.Show instead of writing to a log file. I just call SetupExceptionHandling from each of my scenes.

static bool isExceptionHandlingSetup;
public static void SetupExceptionHandling()
{
    if (!isExceptionHandlingSetup)
    {
        isExceptionHandlingSetup = true;
        Application.RegisterLogCallback(HandleException);
    }
}

static void HandleException(string condition, string stackTrace, LogType type)
{
    if (type == LogType.Exception)
    {
        MessageBox.Show(condition + "

" + stackTrace);
}
}

There is a plugin called Reporter, which would capture your Debug Logs, Crash stacktrace and Screen Shot on Error; then sends you and email. This is a crossplatform plugin , so you would receive the error logs also from user devices. Hope it helps.