Uncaught exception
I have the following code as part of a class I'm building for impersonation. It works, but the UnauthorizedAccessException is being marked as unhandled. If I hit continue, it all works as expected. But why is it unhandled and how can I address that?
6 Replies
you are re-throwing the exception in the final catch block
is that what you mean by unhandled?
incidentally, there is no point in a catch block that only contains a throw statement (your first one)
https://devblogs.microsoft.com/devops/understanding-exceptions-while-debugging-with-visual-studio/:
User-unhandled Exceptions: When a first chance exception is not caught (handled) in user code and is caught in “External Code” on the call stack, this is classified as a “user-unhandled” exception. This classification is only applicable when debugging Managed or JavaScript applications with Just My Codeenabled.So
WindowsIdentity.RunImpersonated
is catching the exception internally, then re-throwing it
VS will break (by default) when an exception is thrown, and is "user-unhandled".I'm throwing the exception within the
RunImpersonated
function. Shouldn't the outer try/catch handle that? VS breaks on the throw new UnauthorizedAccessException
line.I tried to explain above
The body of RunImpersonated is something like (hugely simplified):
Because your exception is being caught by non-user code, it gets counted as user-unhandled. Yes I know you catch it later, but the first time it's thrown it's caught be non-user code
Because it's user-unhandled, VS breaks when it's thrown
Yeah, throwing any exception within
action();
causes that issue. So as long as I don't throw it's fineAt the end of the day, when VS decides to break on an exception is just a best guess. You can untick the little box in the pop-up if it gets it wrong