Exception triggered but not thrown

All game’s logic are some 20 functions which contain commands, all executed from top to bottom, no IEnumerator, no Task. No asynchronous processing as far as I know.

catch (Exception e)
        {
            Debug.Log("before");
            throw new Exception("Error: City spawning has failed: " + e);
            Debug.Log("After");
        }

Note that “before” does appear three times (as it is absolutely expected to fail three times due to other reasons), but not a single exception is shown. Not a single one, “After” doesn’t appear in console either. I desperately need it to actually show itself, because I need to solve whatever is failing.

“After” will never appear in the console. You should be getting a compiler warning that your “After” Debug.Log statement is unreachable. Once an exception is throw, as on line 4 in your code, nothing after that line will be executed.

Are you sure you haven’t simply disabled errors in your console?

5024531--492305--upload_2019-10-2_13-10-28.png

Make sure that button is enabled or you won’t see errors.

1 Like

I don’t remember how the Unity editor handles exceptions, but I assume that if “After” is not shown, then the exception is being thrown even if you can’t see anything. Instead of throwing the exception, maybe you can use Debug.Log or Debug.LogError to see the contents?

If you have it set-up to use your IDE’s debugger with Unity, maybe you can put a break point there, and then you can view the contents of e in the debugger.

1 Like

Good eye, I literally checked it in the first place, but no, it’s literally 0 errors.

It’s not just that I need that single e variable. In general I expect an error to spawn, I don’t know there’s an error, unless there’s an error or something unexpected breaks. I’ve learned so far that throw new Exception() always breaks the processing and throws an error in the console.

I don’t know why throwing the exception wouldn’t work but if you use Debug.LogError it should also cause an error to appear in the console.

1 Like

LogError spawns an error, but doesn’t stop the execution like Exception does. Which is important to me.

What does the rest of the code look like? Is this in an Update() method? What’s calling the function? Depending on what’s triggering the code to run, it’s possible the error handling could be different.

For me, if I make a method throw (in this case, a method within Update() based on a key press event) I get proper logging:

Any chance you’ve set up “Application_logMessageReceived” in your project? (Unity - Scripting API: Application.logMessageReceived) That would cause unhandled exceptions to be handled by some other code. I use this to handle errors in standalone builds:

        private void Application_logMessageReceived(string condition, string stackTrace, LogType type)
        {
            if (type == LogType.Error || type == LogType.Exception)
            {
#if UNITY_EDITOR
                // Don't pop up the dialog in the editor.
                return;
#else
                IsHandlingGlobalError = true;

                SubscriptionDirector.Instance.Publish(SubscriptionType.LaunchSubmitFeedbackDialog, new LaunchSubmitFeedbackDialogSubscriptionPayload()
                {
                    IsFatalError = true,
                    ExceptionMessage = condition,
                    StackTrace = stackTrace
                });
#endif
            }
        }
1 Like

It an Awake() function, that calls a void method, which has a list of constructed classes e.g. MyClass myClass = new MyClass("something");. This Exception is ignored in constructor of one of these classes.

That what I was expecting as well.

No, I don’t actually, I just checked using Visual Studio, and nope.

I really don’t think I’ve changed anything crazy, but something must have happened.

Edit:
Heck, with Visual Studio I managed to fix the error, but I’m still staunch on finding out what went wrong, I’ll keep a comment with bugged code in premises. I still need to fix it, what if it will happen again and I won’t know, and my entire application just tumbles? What went wrong? Why didn’t Exception throw?

You can also use Debug.Break to pause the execution when the error occurs.

1 Like

That’s not a real option to me, because I need it to both print string and stop execution. Two liner of “Debug.Break” and “return” looks like pretty than a professional “throw new Exception();”.