is there a way to catch global application exceptions?

Is it possible to catch all the unhandled exceptions in a centralized place?
i.e. let’s say that the game could through server connection exceptions in several places, I want them to be caught from an unique place, for example to show a message box.

3 Answers

3

You can use Application.RegisterLogCallback to get everything that goes into the unity log - it gives you the type, the stack trace and the logging string of exceptions and, of course, a bunch of other things - would that help you?

yes I thought about that, but I am not sure it is a good idea. I mean if I cannot use catch, I will not either use throw then. I could implement something on my own (like a messaging system that works in a similar way)

Yeah, understood - thought it was worth pointing out.

thank you a lot anyway :)

I mark it as correct, because eventually it was a correct answer

This was exactly what I was looking for. It is deprecated however, the new callback is "Application.logMessageReceived". So you would do: private void Awake(){ Application.logMessageReceived += handleUnityLog; } // then private void handleUnityLog(string logString, string stackTrace, LogType type) { // do stuff }

using UnityEngine;
using System;

public class test : MonoBehaviour {
    public string output = "";
    public string stack = "";
    void OnEnable() {
        Application.RegisterLogCallback(HandleLog);
    }
    void OnDisable() {
        Application.RegisterLogCallback(null);
    }
    void HandleLog(string logString, string stackTrace, LogType type) {
        output = logString;
        stack = stackTrace;
    }
}

That sounds like a bad idea.

Nevertheless, these pages discuss some options:

That Stack Overflow thread is not primarily discussing the Mono runtime, so may not apply.

oh thanks for the answer...yeah in c# it is easy, but I was wondering how to do it in the Unity Environment. In a standard c# application I have the concept of mainform, which I do not have in Unity.