使用 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)

#include <exception>

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

在 Visual Studio 中构建此代码会触发 C4297 警告(假设函数不会引发异常但确实会引发异常)。

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 语句中的 return 是否应防止异常被重新抛出?

我记录了 bug but they closed it as duplicate。另一个 bug 没有 return statement,但我认为它有很大的不同。

Live example

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

我相信你是。其一,明确指出在构造函数中,函数尝试块的处理程序可能不包含 return 语句。

[except.handle]

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

明确离开此类处理程序的唯一方法是抛出另一个异常。 return 语句被禁止正是因为它会吞噬异常。当我们通过 end

的流动隐式离开处理程序时

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] 中的位表示到达 void returning 函数的右括号等同于 return;在末尾。所以第一句话告诉我们,在析构函数的函数尝试块的处理程序中,结尾的流动不是 return;,它会重新抛出。那里没有隐含的 return。

这只留下结论,明确 returning,由于未被禁止,必须吞下当前异常。