收到COMException捕获特定的错误代码 [英] Catching COMException specific Error Code

查看:360
本文介绍了收到COMException捕获特定的错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我。我得从COM,我需要捕捉,然后尝试别的做的事,别人都应该忽略一个特定的异常。唯独我的错误信息是:

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 message with the Exception is:

System.Runtime.InteropServices.COMException
(0x800A03EC):Microsoft Office Excel中
无法访问该文件'C:\test.xls。
有几种可能的原因:

System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Office Excel cannot access the file 'C:\test.xls'. There are several possible reasons:

所以,我最初的尝试是

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

不过后来我读了编译器警告:

However then I read a compiler warning:

警告22比较积分
常数是没用的;不变的是类型
'廉政'的范围之外
..... ExcelReader.cs 629 21

Warning 22 Comparison to integral constant is useless; the constant is outside the range of type 'int' .....ExcelReader.cs 629 21

现在我知道0x800A03EC是
HResult的和我刚刚看了MSDN上的
和阅读:

Now I know the 0x800A03EC is the HResult and I've just looked on MSDN and read:

HRESULT是一个32位的值,
分为三个不同的字段:
程度码,设备代码,和一个
错误代码。严重性代码
表示的返回值
是否代表信息,警告或
错误。该工厂代码标识
负责
错误的系统的区域。

HRESULT is a 32-bit value, divided into three different 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.

所以,我的终极问题,是我怎么保证我陷阱,特定的异常?或者,我怎么从HRESULT中的错误代码?

So my ultimate question, is how do I ensure that I trap that specific exception? Or how do I get the error code from the HResult?

在此先感谢。

推荐答案

的错误码应该是一个无符号整数;

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 
    }
}

这篇关于收到COMException捕获特定的错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆