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?
c#
try {
WindowsIdentity.RunImpersonated(_safeAccessTokenHandle, () => {
// This will throw if the impersonation fails
//string impersonatedUser = WindowsIdentity.GetCurrent().Name;
//Console.WriteLine("Impersonation successful, current user: " + impersonatedUser);

try {
string securePath = "";
if (!Directory.Exists(securePath)) {
throw new UnauthorizedAccessException($"Access to {securePath} was denied.");
}
} catch {
throw;
}
});
} catch (Exception ex) {
Console.WriteLine($"Impersonation validation failed: {ex.Message}");
Dispose();
// Re-throw to ensure the exception is handled by the caller
throw;
}
c#
try {
WindowsIdentity.RunImpersonated(_safeAccessTokenHandle, () => {
// This will throw if the impersonation fails
//string impersonatedUser = WindowsIdentity.GetCurrent().Name;
//Console.WriteLine("Impersonation successful, current user: " + impersonatedUser);

try {
string securePath = "";
if (!Directory.Exists(securePath)) {
throw new UnauthorizedAccessException($"Access to {securePath} was denied.");
}
} catch {
throw;
}
});
} catch (Exception ex) {
Console.WriteLine($"Impersonation validation failed: {ex.Message}");
Dispose();
// Re-throw to ensure the exception is handled by the caller
throw;
}
6 Replies
Becquerel
Becquerel2w ago
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)
canton7
canton72w ago
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".
engineertdog
engineertdog2w ago
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.
canton7
canton72w ago
I tried to explain above The body of RunImpersonated is something like (hugely simplified):
void RunImpersonated(,..., Action action)
{
try
{
action();
}
catch
{
// ...
throw;
}
}
void RunImpersonated(,..., Action action)
{
try
{
action();
}
catch
{
// ...
throw;
}
}
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
engineertdog
engineertdog2w ago
Yeah, throwing any exception within action(); causes that issue. So as long as I don't throw it's fine
canton7
canton72w ago
At 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