Try/Catch block doesn't work?

Make such extended class

public class InputModuleEx : StandaloneInputModule
{
    public override void Process()
    {
        try
        {
            base.Process();
        }
        catch (Exception ex)
        {
            Debug.LogWarning("Exception caught: " + ex.Message);
        }
    }
}

And replace with it the one in EventSystem game object. Throw exception in script attached to button click. It doesn’t work :frowning:

3281940–253903–Exceptions doesnt work.zip (9.9 KB)

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

What constructor? There is override of Process method.

I’m sorry I did misread that :frowning: I don’t know how I got that sorry again

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.

To confirm I tried this and a button click was logged between those two calls.

Debug.Log("Before Process");
base.Process();
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.

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.

1 Like

Probably thats the case.