Search Unity

How to handle FATAL EXCEPTIONS

Discussion in 'Scripting' started by jl008, May 1, 2018.

  1. jl008

    jl008

    Joined:
    Jan 31, 2013
    Posts:
    25
    Hi there,

    So one of the 3rd-party native extension is acting up. I get "Unfortunately X has stopped" error, and in the catlog I see:

    D/AndroidRuntime(23509): Shutting down VM
    E/AndroidRuntime(23509): FATAL EXCEPTION: main

    Is there a way to catch those exceptions and handle them gracefully?

    I have Application.logMessageReceived += handleException; in my main class. But it doesn't get to it.
    All calls to native api also wrapped in try catches.

    So is there a way? Or because it happens at a deeper level, there is nothing you can do with Unity?
     
  2. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    If it's happening from a call in your scripts, even if it's low level, you should be able to catch and handle it. If it's coming from something in Unity, possibly caused by an incompatibility you won't be able to.

    Can you reproduce the error on a dev machine? Have you tried attaching the debugger with the Break on Exception's all checked off?

    If you can't get it on a dev machine, maybe you can set up a debug version on the android device and attach a debugger. I don't have any experience there but it should be possible.

    You need to isolate the call that throws the exception and then show us how you try to catch it.

    I believe Application.logMessageReceived will only trigger if something is sent to the debug output. If you have a serious enough fatal exception, maybe Unity doesn't even try to do this.

    AppDomain.UnhandledException might be able to catch the exception but it depends if another mechanism in Unity is catching unhandled exceptions in your user code and then doing its own thing, like logging and then closing the app in a way that appears graceful to .net. I simply don't know enough about the Unity mechanism for that.
     
  3. jl008

    jl008

    Joined:
    Jan 31, 2013
    Posts:
    25
    @eisenpony Thanks for the reply.

    I knew what caused the issue in my case. A native plugin was missing a jar lib. So at run-time the the JVM was throwing "Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat" and quitting the app. Adding the missing jar solved the issue.

    But I still wanted to ask the question, because I don't know what else can cause a "FATAL EXCEPTION: main" and wanted to handle it (catch it, report it via analytics, and then ignore it).

    Here is a hypothetical example: A fatal run-time exceptions is happening on some devices but not others (for whatever reason). Let's also assume that it is because of peculiarities of the native ads plugin. In this case, I want the players to continue playing and I will be fine with ads not showing on these devices.

    Thanks for pointing out AppDomain.UnhandledException I will research it.