• 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 /
  • Help Room /
avatar image
0
Question by dmazzocco · Jun 26, 2018 at 01:26 AM · exception

UnhandledException and FirstChanceException events not being invoked

I'm trying to create a global exception handler, and would like to have a reference to the Exception to do some special handling. Is there a way to receive exceptions like this?

I'm aware of Application.LogCallback, but it does not return the Exception object. ILogHandler.LogException() does provide the Exception, but only if Debug.LogException() is called. UnhandledException gets a callback when an exception is thrown on a different thread, but not the main thread. Sample:

 public class ExceptionHandler : MonoBehaviour, ILogHandler
 {
     private static ILogHandler defaultLogHandler;
 
     public void Start()
     {
         AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
         AppDomain.CurrentDomain.FirstChanceException += OnFirstChanceException;
         defaultLogHandler = Debug.unityLogger.logHandler;
         Debug.unityLogger.logHandler = this;
 
         GetComponent<ExceptionThrower>().ThrowExceptions();
     }
 
     public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
     {
         Debug.LogWarning($"---Unhandled Exception--- {(Exception)args.ExceptionObject}");
     }
 
     public static void OnFirstChanceException(object sender, FirstChanceExceptionEventArgs args)
     {
         Debug.LogWarning($"---First Chance Exception--- {args.Exception}");
     }
 
     public void LogFormat(LogType logType, Object context, string format, params object[] args)
     {
         defaultLogHandler.LogFormat(logType, context, format, args);
     }
 
     public void LogException(Exception exception, Object context)
     {
         Debug.LogWarning($"---LogException--- {exception}");
     }
 }

 public class ExceptionThrower : MonoBehaviour
 {
     public void ThrowExceptions()
     {
         this.StartCoroutine(this.ExceptionCoroutine());
 
         Debug.LogException(new Exception("Debug.LogException"));
 
         new Thread(() => 
                 {
                     try { throw new Exception("Caught Exception on spawned thread."); }
                     catch (Exception) {}
                 }).Start();
 
         new Thread(() =>
                 {
                     throw new Exception("Unhandled Exception on spawned thread.");
                 }).Start();
 
         try { throw new Exception("Caught exception on main thread."); }
         catch (Exception) {}
 
         throw new Exception("Unhandled Exception on main thread.");
     }
 
     private IEnumerator ExceptionCoroutine()
     {
         yield return null;
 
         try { throw new Exception("Caught exception on coroutine."); }
         catch (Exception) {}
 
         throw new Exception("Unhandled Exception on coroutine.");
     }
 }

 Output:
 ---LogException--- System.Exception: Debug.LogException
 Exception: Unhandled Exception on main thread.
 ---Unhandled Exception--- System.Exception: Unhandled Exception on spawned thread.
 Exception: Unhandled Exception on coroutine.
Comment
Add comment · Show 1
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 VPiris · Sep 23, 2019 at 09:58 AM 0
Share

Hello, Did you solve it ?

I know i try to wake up a dead subject but i'm at the same point so i wont $$anonymous$$d trying a bit necromancy if it can help me.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by jhughes2112 · Mar 31, 2020 at 07:52 PM

Yes. Still a problem. The best I have found is to detect that a main thread exception is about to be reported and do something with the report (you won't have access to the exception object, just the text).

             Application.logMessageReceived += logMsgRecv;
             Application.logMessageReceivedThreaded += logMsgRecv;
     
     ...
     
         // When Unity log receives a message, this gets an event called.  We can tell if an exception happens this way
         public void logMsgRecv(string _condition, string _stackTrace, LogType _type)
         {
             if (_type==LogType.Exception)
             {
                 ... do whatever you want ...
             }
         }
     
 

What I would mention is that, if you look at the call stack you recevied in this event calllback, it's pretty clear that Unity itself has a try/catch and their own handling inside coroutines and main thread C# execution. See here:

 (Mono JIT Code) [CrashHandler.cs:60] CrashHandler:logMsgRecv (string,string,UnityEngine.LogType)
 (Mono JIT Code) [Application.cs:121] UnityEngine.Application:CallLogCallback (string,string,UnityEngine.LogType,bool)
 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void_object_object_int_byte (object,intptr,intptr,intptr)
 (mono-2.0-bdwgc) [mini-runtime.c:2809] mono_jit_runtime_invoke 
 (mono-2.0-bdwgc) [object.c:2919] do_runtime_invoke 
 (mono-2.0-bdwgc) [object.c:2966] mono_runtime_invoke 
 (Unity) scripting_method_invoke
 (Unity) ScriptingInvocation::Invoke
 (Unity) ScriptingInvocation::Invoke<ScriptingObjectPtr>
 (Unity) Application_Bindings::LogCallbackImplementation
 (Unity) DebugStringToFilePostprocessedStacktrace
 (Unity) DebugStringToFile
 (Unity) Scripting::LogException
 (Unity) MonoBehaviour::InvokeMethodOrCoroutineChecked
 (Unity) MonoBehaviour::Start

My guess is there's no such thing as an unhandled exception on the main thread, which is why you aren't going to get that callback.

Best, JH

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

145 People are following this question.

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

Related Questions

Invalid PBX project - iOS Build Help! 2 Answers

Variable has been assigned but has UnassignedReferenceExcecption 1 Answer

Problem with Lists and Remove 0 Answers

Facing Null Reference Exception 0 Answers

Error in child particle systems (2017 2.0b11) 1 Answer


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