3
try
{
    value = System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName; 
}       
catch (Exception e)
{
    //handle exception
}

I am trying to call a method from System.DirectoryServices.AccountManagement.ni.dll to locate the UPN for the user to authenticate the client. However, in the case where the user's password has been changed by an administrator, this dll is throwing an exception and I can't figure out why catch(Exception e) is not handling it. The exception is bubbling up and taking down my application.

Is there something else I need to do to catch this exception?

Hampton Terry
  • 344
  • 1
  • 13
  • 1
    Unmanaged exceptions should be caught in the unmanaged DLL. You cannot expect to catch them in your managed code. You'll need to fix the DLL. – David Heffernan Jan 22 '14 at 23:05
  • The problem is this is not my dll, this is a Microsoft dll http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal(v=vs.110).aspx – Hampton Terry Jan 22 '14 at 23:18
  • That is a managed DLL. Where is the unmanaged DLL? – David Heffernan Jan 22 '14 at 23:19
  • Sorry my mistake. It is a managed dll that is throwing the exception. – Hampton Terry Jan 22 '14 at 23:22
  • 1
    If you cannot catch the exception, what makes you so sure that it is being thrown? – David Heffernan Jan 22 '14 at 23:26
  • Faulting module path: C:\Windows\assembly\NativeImages_v4.0.30319_32\System.DirectorySer#\46a7f51ef1a9d917598b96f7a758a459\System.DirectoryServices.AccountManagement.ni.dll I can also put trace statements directly before and after the call, the second one does not show up – Hampton Terry Jan 22 '14 at 23:31
  • That doesn't look like a managed exception. Do you have any more details? This sort of stuff is not much use in comments. Add the detail to the question please. – David Heffernan Jan 22 '14 at 23:33
  • @HamptonTerry please include a full copy of the text of the error message you have logged, also please show the logging code to demonstrate that you narrowed the issue down. Lastly please add a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) as you are getting a error in that you should not be getting so you will need to walk us step by step in order for us to re-create the problem on our machines to diagnose what is wrong on your machine. If you can not describe how to recreate the issue the best answer you can get is "It should have worked" – Scott Chamberlain Jan 23 '14 at 01:10
  • Does this answer your question? [Can you catch a native exception in C# code?](https://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-sharp-code) – JumpingJezza Jan 07 '22 at 03:20

1 Answers1

0

If the catch block in the code in the question is not being entered, then the only conclusion is that

value = System.DirectoryServices.AccountManagement.UserPrincipal.
    Current.UserPrincipalName; 

does not throw an exception. Perhaps during the execution of this statement an exception is thrown and then caught. But the only sane conclusion that can be reached is that your catch-all handler is not catching anything is that there is nothing to be caught.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490