提问人:user3188445 提问时间:9/23/2023 更新时间:9/23/2023 访问量:49
如果要终止程序,从 C++ noexcept 函数中抛出异常是否安全?[复制]
Is it safe to throw exceptions from within C++ noexcept functions if you want to terminate the program? [duplicate]
问:
通过函数传递异常应该终止程序。我的问题是,在您可能遇到导致引发异常的程序错误的情况下,依赖此行为是否安全合理。noexcept
一个简单的例子是,当您想在已验证和未验证的输入上调用相同的函数时,如下所示:
#include <stdexcept>
struct tree {};
tree
parse(char *p)
{
if (*p)
throw std::length_error("only 0-length arguments allowed");
return {};
}
struct validated {
char *buf_;
validated(char *buf) : buf_(buf) { parse(buf_); }
};
tree
parse(const validated &v) noexcept
{
return parse(v.buf_);
}
在上面的代码中,我想是.当然,代码中可能存在一个错误,我在验证缓冲区后对其进行了修改。在这种情况下,我希望程序像断言失败一样崩溃并转储核心,但我想避免未定义或其他可利用行为的风险,这些行为可能比程序崩溃更糟糕。parse(const validated &v)
noexcept
答: 暂无答案
评论