Visual studio - break on exception

I have no problem breaking VS on placed break points when debugging but if unity code throws an exception then Unity can be made to pause the game and show where the error occurred in the console…
but I want it to switch to VS and show the current call stack, variable values etc. - should I be able to?

As it is, I have to repeat the actions with a new breakpoint at the point of exception to get a proper look at the error in VS, seems a Unity limitation

In Visual Studio, go into Debug / Windows / Exception Setting, then enable the managed exceptions you want the debugger to break on.

2 Likes

Thanks! Not sure why I do not know this, but could be mentioned in Unity debugging notes

Sorry to necro this, but is the mentioned setting above really the solution?

Would also like that when the NullReferenceException occurs (unintialized variable etc.), the debugger should stop to show me which variable is the culprit (since the error log does only provide the line and sometimes have multiple reference sin the same line).

However despite this (which already was enabled) no exceptions are thrown.

Breakpoints do work, so the “Attach to Unity” part is successful.

VS version: 2022.

Any ideas?

There’s a separate, Unity-specific exception called “MissingReferenceException” which is thrown in the Editor when a reference to a Unity object is a “fake null”.

Also, sometimes I have to toggle the exception off then on again after attaching the debugger for it to stick.

1 Like

For folks wondering how to add the MissingReferenceException in VS:
Right click “Common Language Runtime Exceptions” in the screenshot above, hit the plus-icon and add “UnityEngine.MissingReferenceException”

And yes, I’ve also seen a lot of flakyness with getting this to work reliably like needing to toggle exceptions on and off to get it to work (never had a problem with any other targets than Unity).

2 Likes

Toggling the exception on and off did the trick for me