是否保证未捕获的异常消息
以下代码是否
#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
方法并打印错误消息吗?
Is the following code
#include <stdexcept>
int main() {
throw std::runtime_error("foobar");
}
guaranteed to produce the following outout?
terminate called after throwing an instance of 'std::runtime_error'
what(): foobar
fish: Job 1, './a.out' terminated by signal SIGABRT (Abort)
Can I rely on this exact output on every compiler?
Can I rely on that what
method will be called and error message will be printed will be printed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,不保证,未指定是否有任何消息。来自 cppreference:
这里相关的情况是“逃逸[...]主函数的异常”。不过,对 std::terminate 的调用是有保证的。您可以安装
std::terminate_handler
如果您愿意,可以打印自定义消息。No it is not guaranteed, it unspecified whether there is any message. From cppreference:
The case that is relevant here is "exceptions that escape [...] the main function". The call to
std::terminate
is guaranteed though. And you can install astd::terminate_handler
to print a custom message if you like.