exception specification

来自cppreference.com
< cpp‎ | language

 
 
C++语言

|- class="t-nv-h1" | colspan="5" | 一般主题 |- | |- |

预处理器
注释

|

|-

|- class="t-nv-h1" | colspan="5" | 流程控制 |-

条件执行语句

| |- |

|

|-

循环语句

| |- |

|

|-

跳转语句

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 函数 |-

函数声明 Lambda函数声明 inline说明符 异常说明符 (已弃用) noexcept说明符 (C++11)

|- class="t-nv-h1" | colspan="5" | Exceptions |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 命名空间 |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类型 |- | |- |

|

|-

说明符

| |- |

decltype (C++11)
auto (C++11)
alignas (C++11)

|

|-

存储持续期说明符 Initialization

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 表达式 |- | |- |

值类别
求值顺序

|

|-

替代表示 字面量 布尔型 - 整型 - 浮点型 字符型 - 字符串 - nullptr (C++11) 用户定义 (C++11)

|- class="t-nv-h1" | colspan="5" | Utilities |-

属性 (C++11) Types typedef声明 类型别名声明 (C++11) Casts 隐式类型转换 - 显示类型转换 static_cast - dynamic_cast const_cast - reinterpret_cast 内存分配

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类 |- | |- |

|

|-

类特有的函数属性

| |- |

|

explicit (C++11)
static

|-

静态成员函数

| |- |

|

|- |- class="t-nv-h1" | colspan="5" | 模板 |- | |- |

|

|- |- class="t-nv-h1" | colspan="5" | 杂项 |- | |- |

|

|-

 
列出的功能的异常可能直接或间接地扔.
原文:
Lists the exceptions that a function might directly or indirectly throw.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

目录

[编辑] 语法

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.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
如果函数抛出一个异常,它的异常规范中未列出的类型,功能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.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 示例

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()
}


[编辑] 另请参阅

来自“http://zh.cppreference.com/mwiki/index.php?title=cpp/language/except_spec&oldid=38207