How to check ErrorCode for REGDB_E_CLASSNOTREG?

那年仲夏 提交于 2019-12-22 11:37:17

问题


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?


回答1:


Use the unchecked keyword:

        catch (COMException ex) {
            if (ex.ErrorCode == unchecked((int)0x80040514)) {
                //...
            }
        }



回答2:


Comparing with its integer equivalent works fine:

if (e.ErrorCode == -2147287036) // REGDB_E_CLASSNOTREG.
{
   // handle this error.
}



回答3:


You can also try by using some text that is displayed in Exception message/Error Message like follows

try
 {  
   // call to Com Method
 } 
catch (COMException e) 
{ 
    if (e.ToString().Contains("Your Error Text here")) // REGDB_E_CLASSNOTREG. 
    {   
     // handle this error.
    }
 } 


来源:https://stackoverflow.com/questions/7790203/how-to-check-errorcode-for-regdb-e-classnotreg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!