AppDomain.UnhandledException on Android

AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs eventArgs) => {
    // ....
}

On iOS, I enable Fast but No Exceptions, and Silent Exit, and then the above callback is triggered as expected. On Android, the callback is never called.

How do I get it to work?

I need the app to crash on uncaught exceptions, just like iOS.

bump

bump

I don’t think this is possible. Unity “catches” all managed exceptions silently and doesn’t crash the game in those cases.

Not on iOS, and it doesn’t have to on Android either.

When my app throws an exception, for which I haven’t explicitly written code to recover from, then my app ends up in an invalid state. This leads to user-data corruption in many cases.

yes, i meant Android. that’s how it is currently…

Then it should be fixed, because not everyone wants their app to continue limping along in an invalid state when an uncaught exception happens. This behaviour is fine in editor, but not for release mode.

I agree. Did you report this as a bug / feature suggestion?

To be honest, the 99999 other feature suggestions are very discouraging. It would be nice if there was some kind of fast track for things that were either important, or in this case, trivial to implement.

I suppose I will do the feature request, but to be honest, I seems naive to think it will get done within the next two years, if at all, or before someone at UT has simply had a stroke of genius on their own.

I am also very active in bug or feature request reporting. With features, the usual reply i get is “report this to the feedback.unity3d.com site”. That site is a nice idea, but i don’t think it gives a right indication of what feature most people want or what is important enough to developers.

I thought for a second that I had found a solution…but it appears to be broken.

The example here shows how to catch the actual exceptions:

This seems to catch exceptions thrown from a UI button callback, but not exceptions thrown in Update(). This can’t be the intended behavior…right?

Even AppDomain.CurrentDomain.UnhandledException will not let you catch exceptions. you only get notified about them (same with the logger i think).

I don’t mean catch as in try/catch.

I mean that the following function will get called when I throw an exception from the callback of a unity UI button(the new UI), but that this function never gets called if I throw an exception from Update() of a MonoBehavior.

void ILogHandler.LogException (Exception exception, UnityEngine.Object context)

What about the good old Application.logMessageReceived ? did you try using it ?

I’m using it for now, but all you get is Unity’s pre-formatted message, instead of the exception itself.

Also, I’m using Process.GetCurrentProcesss().Kill() to kill the process upon receiving the callback, but I don’t think there is any guarantee that Application.logMessageReceived will be called immediately upon the exception being thrown, and before any other script code has been allowed to run.

I went down this rabbit hole just now. Any luck with this? I’d like the UnhandledException to be properly used on Android too. As mentioned earlier, I can’t believe there’s no option for this considering it’s trivial.

3 Likes