是否可以输入两次terminate_handler?
我在终止处理程序中进行了一些清理,并且可能会引发异常。我是否需要担心捕获它以防止递归调用终止处理程序?对于 gcc,这似乎不可能发生,我们只是进入中止状态。标准是这样还是行为未定义?
I have some clean up in a terminate_handler and it is possible to throw an exception. Do I need to worry about catching it to prevent recursive calls to the terminate_handler? With gcc, it seems this can't happen and we just go into abort. Is that true of the standard or is the behavior undefined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
终止处理程序不允许返回(§18.6.3.1/2);它必须结束程序(默认处理程序调用
abort()
)。如果它包括:您将得到未定义的行为,因为您将在没有终止程序的情况下离开该函数(因为异常传播)。因此,如果您有可能抛出的代码,请确保捕获所有异常,如下所示:
但是(回答标题问题),我没有看到任何限制它再次输入的内容,所以这在技术上应该没问题:
A terminate handler is not allowed to return (§18.6.3.1/2); it must end the program (the default handler calls
abort()
). If it consisted of:You'd get undefined behavior, because you would leave the function (because the exception propagates) without having terminated the program. So if you have code that could throw, make sure you catch all exceptions, like this:
However (to answer the title question), I don't see anything that restricts it from being entered again, so this should be technically be fine:
不,您无法从
std::terminate
恢复正常的程序流程。但是,您可以从意外
函数中抛出不同的异常。Terminate
就是这样做的——程序执行在完成后终止。编辑:
也就是说,你不应该在
std::terminate
中做任何复杂的事情 - 如果你在terminate
中,那么事情已经足够严重了,你不应该这样做尝试继续 - 并且出于同样的原因,您不应该尝试在std::terminate
中分配内存之类的事情 - 如果程序由于内存不足而在那里怎么办健康)状况?你对此无能为力。No, you cannot resume normal program flow from
std::terminate
. You can, however, throw a different exception from theunexpected
function.Terminate
does just that -- program execution terminates after it completes.EDIT:
That said, you shouldn't be doing anything complicated in
std::terminate
-- if you're interminate
then things have blown up sufficiently that you should not be trying to continue -- and you shouldn't try to do things like allocate memory instd::terminate
for the same reason -- what if the program is in there as a result of a low memory condition? There's nothing you can do about it there.