C# 捕获 COMException 特定的错误代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1426147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:24:17  来源:igfitidea点击:

Catching COMException specific Error Code

c#com

提问by Ian

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:

我希望有人可以帮助我。我有一个来自 COM 的特定异常,我需要捕获它然后尝试做其他事情,所有其他异常都应该被忽略。我的异常错误消息是:

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

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

So my initial attempt was

所以我最初的尝试是

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

However then I read a compiler warning:

但是后来我读到一个编译器警告:

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

警告 22 与积分常数比较是无用的;常量超出了“int”类型的范围.....ExcelReader.cs 629 21

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

现在我知道 0x800A03EC 是 HResult 并且我刚刚查看了 MSDN 并阅读了:

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 是一个 32 位值,分为三个不同的字段:严重性代码、设施代码和错误代码。严重性代码指示返回值是表示信息、警告还是错误。设施代码标识了对错误负责的系统区域。

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?

所以我的最终问题是,我如何确保捕获该特定异常?或者如何从 HResult 中获取错误代码?

Thanks in advance.

提前致谢。

采纳答案by Paolo Tedesco

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

ErrorCode 应该是一个无符号整数;您可以按如下方式进行比较:

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

回答by Ian

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

我实际上设法让它在我需要的系统上运行,发现错误代码是 -2146807284。

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

看一下,如果我将 0x800A03EC 转换为二进制,然后将其视为 2 的恭维,那么您可以计算该值。

回答by Scoobie

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 codeidentifies the area of the system responsible for the error. The error codeis 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

HRESULT 值有 32 位,分为三个字段:严重性代码、设施代码和错误代码。严重性代码指示返回值是表示信息、警告还是错误。该设备代码标识负责错误的系统的区域。该错误代码是一个唯一的编号分配给代表例外。每个异常都映射到一个不同的 HRESULT。 摘自: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.

据我所知,HRESULT 位的前半部分可能会根据导致异常的系统/进程而改变。后半部分包含错误类型。

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.

注意:请记住,我不是 .NET 人,所以请注意上面代码中的语法错误。