exception specification
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
列出的功能的异常可能直接或间接地扔.
原文:
Lists the exceptions that a function might directly or indirectly throw.
目录 |
[编辑] 语法
throw( typeid, typeid, ...)
|
(已弃用) | ||||||||
[编辑] 解释
如果一个函数被声明为与它的异常规范中列出的
T
类型,函数可能抛出异常,该类型或从它派生的类型.原文:
If a function is declared with type
T
listed in its exception specification, the function may throw exceptions of that type or a type derived from it.如果函数抛出一个异常,它的异常规范中未列出的类型,功能std::unexpected被调用。默认的函数调用std::terminate,但它可能会被替换由用户提供的功能(通过std::set_unexpected),可致电std::terminate或者抛出一个异常。如果所接受的异常规范,从std::unexpected抛出的异常堆栈平仓像往常一样继续。如果不是这样,但std::bad_exception所允许的异常规范,std::bad_exception被抛出。否则,std::terminate被称为.
原文:
If the function throws an exception of the type not listed in its exception specification, the function std::unexpected is called. The default function calls std::terminate, but it may be replaced by a user-provided function (via std::set_unexpected) which may call std::terminate or throw an exception. If the exception thrown from std::unexpected is accepted by the exception specification, stack unwinding continues as usual. If it isn't, but std::bad_exception is allowed by the exception specification, std::bad_exception is thrown. Otherwise, std::terminate is called.
[编辑] 示例
class X {}; class Y {}; class Z : public X {}; class W {}; void f() throw(X, Y) { int n = 0; if (n) throw X(); // OK if (n) throw Z(); // also OK throw W(); // will call std::unexpected() }