2
try
{
    // call to Com Method
}
catch (COMException e)
{
    if (e.ErrorCode == 0x80040154) // REGDB_E_CLASSNOTREG.
    {
       // handle this error.
    }
}

I would like to check if com exception is thrown due to REGDB_E_CLASSNOTREG then handle it. I tried with the code above but it gives warning:

Comparison to integral constant is useless; the constant is outside the range of type 'int'

I believe this error is due to 0x80040154 is not in Int32 range.

Can you suggest any possible solution? or Is there any other way to check this?

4

3 回答 3

3

使用未选中的关键字:

        catch (COMException ex) {
            if (ex.ErrorCode == unchecked((int)0x80040514)) {
                //...
            }
        }
于 2011-10-17T12:46:04.603 回答
1

与其等效的整数进行比较可以正常工作:

if (e.ErrorCode == -2147287036) // REGDB_E_CLASSNOTREG.
{
   // handle this error.
}
于 2011-10-17T12:11:14.297 回答
0

您也可以尝试使用异常消息/错误消息中显示的一些文本,如下所示

try
 {  
   // call to Com Method
 } 
catch (COMException e) 
{ 
    if (e.ToString().Contains("Your Error Text here")) // REGDB_E_CLASSNOTREG. 
    {   
     // handle this error.
    }
 } 
于 2011-10-17T06:46:26.877 回答