为什么重新抛出异常会丢弃"what()"给出的信息?
Why does rethrowing an exception discards the information given by 'what()'?
我在Windows 10上使用MinGW gcc(或g ++)7.1.0。
通常,抛出std::runtime_error
会显示如下信息:
terminate called after throwing an instance of 'std::runtime_error'
what(): MESSAGE
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
但下面的代码只显示最后两行,what()
信息丢失:
#include <stdexcept>
using namespace std;
int main() {
try {
throw runtime_error("MESSAGE");
} catch (...) {
throw;
}
}
所以上面的代码只输出:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
如果我用const exception&
、const runtime_error&
替换...
(或不const
、不带&
或两者兼而有之)也会发生同样的事情。
据我所知,throw;
重新抛出当前捕获的异常。那么为什么没有显示what()
呢?
是什么让你认为重新抛出异常会丢弃"what()"给出的信息?你从不检查what()
重新投掷后返回的内容。 显示This application has requested...
消息是因为未捕获的异常导致程序终止。what()
内容不应自动打印。
您可以通过what()
打印值返回,没有任何问题:
#include <stdexcept>
#include <iostream>
int main()
{
try
{
try
{
throw ::std::runtime_error("MESSAGE");
}
catch (...)
{
throw;
}
}
catch(::std::exception const & exception)
{
::std::cout << exception.what() << ::std::endl;
}
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 如何设置一个范围来提取我想要获得的信息
- 什么是 std::exception::what() 以及为什么要使用它?
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- what(): basic_string::_M_construct null not valid
- what(): basic_string::_M_construct 空无效错误
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- What is unordered_set in C++
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- 为什么重新抛出异常会丢弃"what()"给出的信息?