当我在 C++/CLI 中捕获异常时,为什么会收到 CA1806?
我最近将项目从 Visual Studio 2008 升级到 Visual Studio 2010。
通过在发布中启用代码分析和编译,我收到警告 CA1806:不要忽略方法结果。
我已设法将产生警告的代码减少为以下代码:
.h file:
public ref class Foo
{
public:
void Bar();
};
.cpp file:
void Foo::Bar()
{
try
{
}
catch (const std::exception&) // here I get the warning
{
}
}
the warning:
CA1806:微软。用法: 'Foo::Bar(void)' 调用 '全局::__CxxRegisterExceptionObject(void*, void*)' 但不使用 HRESULT 或该方法返回的错误代码。 这可能会导致意外的行为 在错误情况或资源不足的情况下 情况。将结果用于 条件语句,赋值 结果传递给变量,或者将其作为 另一个方法的参数。
如果我尝试使用异常值或执行catch(...)
,则会出现警告still。如果我捕获托管异常或在调试中编译,我不会收到警告。
为什么我会收到此警告?
更新
我决定打开Microsoft Connect 上的错误报告。
I've recently upgraded my project from Visual Studio 2008 to Visual Studio 2010.
By enabling Code Analysis and compiling in Release, I'm getting warning CA1806: Do not ignore method results.
I've managed to reduce the code that produces the warning to this code:
.h file:
public ref class Foo
{
public:
void Bar();
};
.cpp file:
void Foo::Bar()
{
try
{
}
catch (const std::exception&) // here I get the warning
{
}
}
the warning:
CA1806 : Microsoft.Usage :
'Foo::Bar(void)' calls
'Global::__CxxRegisterExceptionObject(void*,
void*)' but does not use the HRESULT
or error code that the method returns.
This could lead to unexpected behavior
in error conditions or low-resource
situations. Use the result in a
conditional statement, assign the
result to a variable, or pass it as an
argument to another method.
If I try to use the exception value or do catch(...)
the warning still appears. If I catch managed exceptions instead or compile in Debug I don't get the warning.
Why do I get this warning?
UPDATE
I've decided to open a bug report on Microsoft Connect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Visual Studio 2010 的一个错误。
正如您在 错误报告,微软重现了该错误,并决定推迟解决,没有解决办法。
欢迎您为该错误投票,这样微软也许会决定尽快解决它。
This is a Visual Studio 2010 bug.
As you can see in the bug report, Microsoft reproduced the bug and has decided to postpone the resolution with no workaround.
You are welcome to vote for the bug so maybe Microsoft will decide to resolve it sooner.
可能是因为您没有调用任何可能抛出的代码。
Could be because you didn't call any code that could throw.