是否保证未捕获的异常消息
Is uncaught exception message guaranteed
就是下面的代码
#include <stdexcept>
int main() {
throw std::runtime_error("foobar");
}
保证产生以下结果?
terminate called after throwing an instance of 'std::runtime_error'
what(): foobar
fish: Job 1, './a.out' terminated by signal SIGABRT (Abort)
我可以在每个编译器上依赖这个准确的输出吗?
我可以依赖 what
方法将被调用并打印错误消息吗?
不保证,不确定是否有消息。来自 cppreference:
If an exception is thrown and not caught, including exceptions that escape the initial function of std::thread
, the main function, and the constructor or destructor of any static or thread-local objects, then std::terminate
is called. It is implementation-defined whether any stack unwinding takes place for uncaught exceptions.
这里相关的案例是“逃避 [...] 主要功能的异常”。不过,对 std::terminate
的调用是有保证的。如果你愿意,你可以安装一个 std::terminate_handler
来打印自定义消息。
就是下面的代码
#include <stdexcept>
int main() {
throw std::runtime_error("foobar");
}
保证产生以下结果?
terminate called after throwing an instance of 'std::runtime_error'
what(): foobar
fish: Job 1, './a.out' terminated by signal SIGABRT (Abort)
我可以在每个编译器上依赖这个准确的输出吗?
我可以依赖 what
方法将被调用并打印错误消息吗?
不保证,不确定是否有消息。来自 cppreference:
If an exception is thrown and not caught, including exceptions that escape the initial function of
std::thread
, the main function, and the constructor or destructor of any static or thread-local objects, thenstd::terminate
is called. It is implementation-defined whether any stack unwinding takes place for uncaught exceptions.
这里相关的案例是“逃避 [...] 主要功能的异常”。不过,对 std::terminate
的调用是有保证的。如果你愿意,你可以安装一个 std::terminate_handler
来打印自定义消息。