I’m trying to create a global exception handler for my Unity project and would like to have a reference to the Exception to do some special handling.
I’m using AppDomain.CurrentDomain.UnhandledException to catch the exceptions
public void Start()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
throw new Exception("1");
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Debug.Log("MyHandler caught : " + e.Message);
}
If an exception is thrown on a different thread I receive the event and my handler is called, but if an exception is thrown on the main thread my handler is never called.
Is there a way I can capture unhandled exceptions made on the main thread using AppDomain.CurrentDomain.UnhandledException?