Could you, please, tell me how to handle COM exceptions in C# in a right way? For example, I am using DirectorySearcher
and getting the COMException: The server is not operational
. How should I handle this exception? I can write a handler for COMException
, but how I identify particular exception type? Should I examine exception message or HRESULT
for it?
You must look for the HRESULT
, this is the errorcode of the Exception Instance , and therefore the only way to really know what is going on. You can decypher the HRESULT
with this and this article.
Example:
try
{
//Your code
}
catch(COMException ex)
{
int error = ex.ErrorCode;
//Conditions and error handling
}
Basically the HRESULT
is a 32 bit integer where the two most significant bits describe what sort of message it is (succes, info, warning, error). The other 30 bits are used to describe the rest of the message.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.