我可以直接打电话给 std::terminate 吗?
May I call std::terminate directly?
我有一个非空函数,它 return 是一个用作 class 构造函数输入的值,但该值有可能不可用,所以我想要 return 成功时的值,或者在函数 return.
失败时终止 (std::terminate()
) 应用程序
既然std::terminate
是根据https://en.cppreference.com/w/cpp/error/terminate被runtime
调用的,我是否有义务throw an exception
让运行时决定程序是否终止?
我在刚刚执行的简单测试中读到的以下警告怎么办? "terminate called without an active exception"
我是不是走傻了?
提前感谢您的见解!
您链接到的同一页面,在您的问题中,也有以下陈述:
std::terminate() may also be called directly from the program.
... I obliged to throw an exception and let the runtime decide whether the program is terminated or not?
由你决定。如果您希望能够在调用堆栈的某处捕获异常,那么您应该抛出异常。如果你因为无法恢复而想终止,那么调用 std::terminate
就可以了。考虑一下,如果你抛出一个没有被捕获的异常,那么程序无论如何都会终止,所以你可以选择是否捕获它,而 std::terminate
总是终止。
我有一个非空函数,它 return 是一个用作 class 构造函数输入的值,但该值有可能不可用,所以我想要 return 成功时的值,或者在函数 return.
失败时终止 (std::terminate()
) 应用程序
既然std::terminate
是根据https://en.cppreference.com/w/cpp/error/terminate被runtime
调用的,我是否有义务throw an exception
让运行时决定程序是否终止?
我在刚刚执行的简单测试中读到的以下警告怎么办? "terminate called without an active exception"
我是不是走傻了?
提前感谢您的见解!
您链接到的同一页面,在您的问题中,也有以下陈述:
std::terminate() may also be called directly from the program.
... I obliged to throw an exception and let the runtime decide whether the program is terminated or not?
由你决定。如果您希望能够在调用堆栈的某处捕获异常,那么您应该抛出异常。如果你因为无法恢复而想终止,那么调用 std::terminate
就可以了。考虑一下,如果你抛出一个没有被捕获的异常,那么程序无论如何都会终止,所以你可以选择是否捕获它,而 std::terminate
总是终止。