使用function-try-block时在Visual Studio中发出C4297警告(假定函数不会引发异常,但会引发异常) [英] C4297 warning in Visual Studio while using function-try-block (function assumed not to throw an exception but does)

查看:39
本文介绍了使用function-try-block时在Visual Studio中发出C4297警告(假定函数不会引发异常,但会引发异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <exception>

struct FOO
{
  ~FOO() try
  {
    throw std::exception();
  }
  catch (...)
  {
    return; // Shall prevent the exception from being rethrown?
  }
};

在Visual Studio中构建此代码会触发C4297警告(假定函数不会引发异常,但会引发异常).

Building this code in Visual Studio triggers C4297 warning (function assumed not to throw an exception but does).

到达析构函数上的try-block的catch子句的末尾也会自动抛出当前异常,就像通过throw;一样,但允许使用return语句.从cppreference.com引用 ;

Reaching the end of a catch clause for a function-try-block on a destructor also automatically rethrows the current exception as if by throw;, but a return statement is allowed. quoted from cppreference.com;

我是否正确解释了这句话?从catch语句返回是否可以防止重新引发异常?

Do I interpret this sentence correctly? Does return from the catch statement shall prevent the exception from being rethrown?

我登录了

I logged a bug but they closed it as duplicate. The other bug does not have a return statement but I think it makes all the difference.

在线示例

推荐答案

我是否正确解释了这句话?从catch语句返回是否可以防止重新引发异常?

Do I interpret this sentence correctly? Does return from the catch statement shall prevent the exception from being rethrown?

我相信你是.首先,明确指出在构造函数中,function-try-block的处理程序可能不包含return语句.

I believe you are. For one, it is explicitly stated that in a constructor, the handler of a function-try-block may not include a return statement.

[句柄除外]

13 如果在处理程序中出现return语句的构造函数的function-try-block,程序格式错误.

13 If a return statement appears in a handler of the function-try-block of a constructor, the program is ill-formed.

显式离开此类处理程序的唯一方法是引发另一个异常.确切地说,不允许使用return语句是因为它将吞没异常.当我们隐式离开处理程序时,通过结束流动

The only way to explicitly leave such a handler is by throwing another exception. A return statement is disallowed precisely for the reason that it will swallow the exception. When we leave a handler implicitly, by flowing of the end

14 如果控制,当前处理的异常将被抛出到达构造函数的function-try-block的处理程序的结尾,或者析构函数.否则,从复合语句的结尾流出function-try-block的处理程序的等效于该函数的复合语句的结尾(请参见[stmt.return]).

14 The currently handled exception is rethrown if control reaches the end of a handler of the function-try-block of a constructor or destructor. Otherwise, flowing off the end of the compound-statement of a handler of a function-try-block is equivalent to flowing off the end of the compound-statement of that function (see [stmt.return]).

[stmt.return] 中的位表示到达空返回函数的右括号等于最后一个 return; .因此,第一句话告诉我们,在析构函数的function-try-block的处理程序中,结尾的流动不是 return; ,而是重新抛出.那里没有隐含的回报.

The bit in [stmt.return] says that reaching the closing brace of a void returning function is equivalent to a return; at the end. So the first sentence tells us that in a handler of a destructor's function-try-block, flowing of the end is not a return;, it rethrows. There is no implicit return there.

这仅得出一个结论,即由于不被禁止,显式返回必须吞下当前异常.

This leaves only the conclusion that explicitly returning, by virtue of not being prohibited, must swallow the current exception.

这篇关于使用function-try-block时在Visual Studio中发出C4297警告(假定函数不会引发异常,但会引发异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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