- Home /
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.
Answer by whydoidoit · May 30, 2012 at 12:13 PM
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)
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.log$$anonymous$$essageReceived".
So you would do:
private void Awake(){
Application.log$$anonymous$$essageReceived += handleUnityLog;
}
// then
private void handleUnityLog(string logString, string stackTrace, LogType type) {
// do stuff
}
Answer by Bilelmnasser · Jun 07, 2014 at 07:15 PM
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;
}
}
Answer by rutter · Mar 28, 2012 at 10:26 PM
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.