std::rethrow_exception(nullptr) 未定义的行为还是bad_exception?
std::rethrow_exception(nullptr) undefined behavior or bad_exception?
我有一个代码片段,我用nullptr作为参数调用rethrow_exception。 文档说参数应该是非空的,但我想知道,如果我传递 nullptr,行为是未定义的还是已知的?
我每次都bad_exception。但是,此链接表示行为未定义。
std::string msg;
try
{
std::rethrow_exception(nullptr);
}
catch (std::bad_exception &ex)
{
msg = ex.what();
}
catch (std::exception &ex)
{
msg = ex.what();
}
catch (...)
{
msg = "uncaught exception!";
}
任何人,谁能评论到底发生了什么?
这是未定义的行为。
该标准说:
[[noreturn]] void rethrow_exception(exception_ptr p);
前提条件:
p
不是空指针。抛出:
p
引用的异常对象。
违反前提条件是 UB([res.on.required]/2
(。您可能观察到的任何行为都是符合标准的;C++标准对可能发生的事情没有任何限制。所以不要这样做。
相关文章:
- 为重写std::exception的库生成swig接口时出错
- 什么是 std::exception::what() 以及为什么要使用它?
- 链表"exception thrown"
- 为什么程序员同时使用 std::bad_alloc 和 std::exception.是否 std::例外 仅是不够的
- C++ broken_promise exception
- 使用来自 Excel VBA 的 C++ dll 时"Bad DLL calling convention" - 如何解决?
- 请参阅在 Visual Studio 2019 中捕获 std::exception 时对函数模板实例化消息的引用
- QVTKWidget SetRenderWindow() with PCLVisualizer Exception
- 自定义派生的 std::exception 类的 'what' 函数返回神秘的废话
- 使用flag、return、exception、goto或break中止过程
- C++17 编解码器在将标准::字符串转换为标准::字符串时抛出"bad conversion"
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 派生自 std::exception 的类的赋值运算符
- 尝试打印字符串时"Python Exception <class 'gdb.error'> There is no member named _M_dataplus."
- std::exception :使用虚函数和继承与变量
- 对于代理容器上的迭代器来说,"least bad implementation"可能是什么?
- MFC ODBC SQLConfigDataSource() Unhandled Exception
- 这段 C++ 代码的奇怪行为(std::wcout 和 std::exception)
- VTK 7.1.1: vtkX3DExporter exception
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc