• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by sebas77 · Mar 27, 2012 at 01:42 PM · c#exception

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sebas77 · May 30, 2012 at 12:21 PM 0
Share

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)

avatar image whydoidoit · May 30, 2012 at 12:34 PM 0
Share

Yeah, understood - thought it was worth pointing out.

avatar image sebas77 · May 30, 2012 at 12:56 PM 0
Share

thank you a lot anyway :)

avatar image sebas77 · May 30, 2012 at 04:22 PM 0
Share

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

avatar image LiterallyJeff · Mar 25, 2016 at 03:43 PM 3
Share

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
 }
avatar image
0

Answer by rutter · Mar 28, 2012 at 10:26 PM

That sounds like a bad idea.

Nevertheless, these pages discuss some options:

  • http://stackoverflow.com/questions/337702/c-sharp-how-to-implement-one-catchem-all-exception-handler-with-resume

  • http://mono-for-android.1047100.n5.nabble.com/Troubleshooting-an-ANR-Global-Exception-Handler-td5515296.html

  • http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

  • http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/3ac9bb38-5527-476d-bc66-157e35b988b6

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sebas77 · Mar 29, 2012 at 09:01 AM 0
Share

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.

avatar image
3

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Where can I find tutorials for scripting in c#?? 1 Answer

Looking for a Better Way to check Multiple Tags 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges