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;
}
Was this page helpful?