了解C++编程中指定的异常和未经处理的异常
noexcept ReturnType FunctionName(params) noexcept; ReturnType FunctionName(params) noexcept(noexcept(expression); 参数 #include <type_traits> template <typename T> T copy_object(T& obj) noexcept(std::is_pod<T>) { //. . . } 使用 noexcept 代替异常说明符 throw,后者在 C++11 和更高版本中已弃用。当你确信函数绝不允许异常传播到调用堆栈时,我们建议你将 noexcept 应用到函数。使用 noexcept 声明的函数使编译器可以在多种不同的上下文中生成更高效的代码。 未经处理的 C++ 异常 // exceptions_Unhandled_Exceptions.cpp // compile with: /EHsc #include <iostream> using namespace std; void term_func() { cout << "term_func was called by terminate." << endl; exit( -1 ); } int main() { try { set_terminate( term_func ); throw "Out of memory!"; // No catch handler for this exception } catch( int ) { cout << "Integer exception raised." << endl; } return 0; } 输出: term_func was called by terminate. term_func 函数最好是通过调用 exit 来终止程序或当前线程。如果它没有这样做,而是返回到其调用方,则调用 abort。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |