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

Try/Catch block doesn't work?

Discussion in 'Scripting' started by mabakay, Nov 9, 2017.

  1. mabakay

    mabakay

    Joined:
    Jun 10, 2016
    Posts:
    20
    Make such extended class

    Code (CSharp):
    1.  
    2. public class InputModuleEx : StandaloneInputModule
    3. {
    4.     public override void Process()
    5.     {
    6.         try
    7.         {
    8.             base.Process();
    9.         }
    10.         catch (Exception ex)
    11.         {
    12.             Debug.LogWarning("Exception caught: " + ex.Message);
    13.         }
    14.     }
    15. }
    16.  
    And replace with it the one in EventSystem game object. Throw exception in script attached to button click. It doesn't work :-(
     

    Attached Files:

  2. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    You're only try-ing the base class's constructor, any exception thrown handling a button click or other method will be outside your try catch block
     
  3. mabakay

    mabakay

    Joined:
    Jun 10, 2016
    Posts:
    20
    What constructor? There is override of Process method.
     
  4. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    I'm sorry I did misread that :( I don't know how I got that sorry again
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Is the exception actually thrown inside of Process?

    Also check that it's actually an exception. Unity has this horrible habit of handling errors with just logging to console.
     
  6. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    To confirm I tried this and a button click was logged between those two calls.
    Code (CSharp):
    1. Debug.Log("Before Process");
    2. base.Process();
    3. Debug.Log("After Process");
    Don't know a lot about try catch and if this is how it is supposed to work. I guess the nesting is too much there, maybe Try catch is supposed to work if that method throws an exception, and not certain listener methods that are called in it?
    Maybe someone can explain it. I mostly use it in threaded routines.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    If base.Process threw an exception, you wouldn't see "After Process" in the console. So if an exception is happening, it's not directly in base.Process. If base.Process starts a thread, and there's an exception in that thread, the try-catch won't trigger.
     
    McDev02 likes this.
  8. mabakay

    mabakay

    Joined:
    Jun 10, 2016
    Posts:
    20
    Probably thats the case.