Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is there really no way to just capture all exceptions unhandled by my code in Unity?

Discussion in 'Editor & General Support' started by Sharlatan, Nov 21, 2017.

  1. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    There are some exceptions in my game I can silently handle and then there are some that bring the game into an invalid state I can't recover from (plus probably at least a few I didn't even think of that might happen unexpectedly).

    When an exception that brings the game into an invalid state occurs, I'd like to present the user with a short dialog box what went wrong and then shut down the game.

    Problem is, I really can't find a way to catch all unhandled exceptions.

    In the end, Unity seems to handle all exceptions in some kind of way, which is why the UnhandledExceptionEvent doesn't catch anything.

    I know about LogCallback, which is the only thing that comes up in Google as some kind of solution for catching unhandled exceptions in Unity. But that's not practical at all. First, it lacks a lot of information because you don't even get the exception instance itself and second and even more important, it catches all logged exceptions, which includes the exceptions my code catches, logs and handles/recovers from in a try/catch block as well as those that aren't handled by my code and bubble up. This means I cannot tell at all if an exception that comes up in LogCallback was handled by my code or not (unless I stop logging exceptions I can recover from but that can't be the solution).

    I nearly can't believe there's nothing as basic as a hook to just attach to every exception that wasn't handled by my code.

    Am I overlooking something?

    Thanks very much!
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    This might help:
    https://stackoverflow.com/questions/7329785/unhandled-exception-is-not-being-caught-by-the-handlers

    But remember on mobile different platforms have their own way of doing things, so the above might only work where Unity is actually running mono.

    Generally speaking you have two things to solve for. Code that by design can throw an exception, and if you are using a global exception handler for those, god help you. And null reference exceptions, which are trickier but it's still better to just track those down.

    In unity since you can separate code via monobehaviours, having any unknown exception just stop your game is almost always a bad idea. The exception could be something that just effects some minor visual thing. So if you keep separate things separate you are already partitioning things in a way where an exception in one area won't hurt the rest of the game. And from there you just track down the exceptions that do break the game, and fix them. Much better approach.
     
  3. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Thanks for th link (can't try it out right now but I'll give it a try later on) and your insights into exception handling.

    I'm hoping I'm on the right track. My idea isn't to have a global exception handler. I'm planning to handle exceptions locally where possible but if I encounter an unexpected exception in a critical part of the game and the code isn't sure how to handle it, I'd rather like to catch that and shut down the application instead of risking letting the user continue and possibly lose progress because the game is in a faulty state.