Catching COMException specific Error Code

前端 未结 3 932
离开以前
离开以前 2020-12-09 08:45

I\'m hoping someone can help me. I\'ve got a specific Exception from COM that I need to catch and then attempt to do something else, all others should be ignored. My error m

相关标签:
3条回答
  • 2020-12-09 09:01

    The ErrorCode should be an unsigned integer; you can perform the comparison as follows:

    try {
        // something
    } catch (COMException ce) {
        if ((uint)ce.ErrorCode == 0x800A03EC) {
            // try something else 
        }
    }
    
    0 讨论(0)
  • 2020-12-09 09:13

    I actually managed to get it running on the system I needed and found the error code was -2146807284.

    Looking at that, if I convert the 0x800A03EC to Binary, then treat it as 2's compliment, then you can calculate the value.

    0 讨论(0)
  • 2020-12-09 09:16

    An HRESULT value has 32 bits divided into three fields: a severity code, a facility code, and an error code. The severity code indicates whether the return value represents information, warning, or error. The facility code identifies the area of the system responsible for the error. The error code is a unique number that is assigned to represent the exception. Each exception is mapped to a distinct HRESULT. Excerpt from: http://en.wikipedia.org/wiki/HRESULT

    From what I gather, the first half of the HRESULT bits may change depending on the system/process that causes the exception. The second half contains the error type.

    Code should look like:

    try {
        // something
    } catch (COMException ce) {
        if ((uint)ce.ErrorCode & 0x0000FFFF == 0x800A03EC) {
            // try something else 
        }
    }
    

    NOTE: please keep in mind I'm not a .NET guy, so be weary of syntax errors in the above code.

    0 讨论(0)
提交回复
热议问题