throw expression

来自cppreference.com
< cpp‎ | language

 
 
C++语言
大会的主题
原文:
General topics
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
流量控制
原文:
Flow control
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
条件执行语句
原文:
Conditional execution statements
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
迭代语句
原文:
Iteration statements
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
跳转语句
原文:
Jump statements
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
功能
原文:
Functions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
函数声明
lambda函数的声明
函数模板
的历史。内嵌说明
异常规范 (已弃用)
noexcept说明 (C++11)
例外
原文:
Exceptions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
扔表达
命名空间
原文:
Namespaces
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类型
原文:
Types
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
decltype specifier (C++11)
规范
原文:
Specifiers
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
CV符
存储时间说明符
constexpr说明 (C++11)
汽车符 (C++11)
alignas说明 (C++11)
初始化
原文:
Initialization
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
原文:
Literals
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
布尔文字
nullptr (C++11)
用户定义的 (C++11)
表达式
原文:
Expressions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
另一种表示形式
实用工具
原文:
Utilities
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类型
原文:
Types
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
typedef declaration
声明类型别名 (C++11)
属性 (C++11)
施放
原文:
Casts
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
隐式转换
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-风格和功能转换
内存分配
原文:
Memory allocation
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
原文:
Classes
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类特定的功能特性
原文:
Class-specific function properties
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
虚函数
覆盖说明 (C++11)
最后说明 (C++11)
明确的 (C++11)
静态的
特殊的成员函数
原文:
Special member functions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
模板
原文:
Templates
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类模板
函数模板
模板特化
参数包 (C++11)
杂项
原文:
Miscellaneous
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
内联汇编
 
信号错误的条件和执行错误处理程序.
原文:
Signals an erroneous condition and executes an error handler.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

目录

[编辑] 语法

throw expression (1)
throw (2)

[编辑] 解释

1)
throw表达式构造一个临时对象存储在未指定的,具有相同的类型作为expression(cv修饰符除去类型转换,从一系列的T指向T的指针,并从返回T指向函数返回T的函数,如必要) ,并把它初始化expression以同样的方式初始化函数的参数或返回值函数的参数或参数的return表达(如复制的省略和移动建设的地方,如果可能的)。异常对象将一直持续到最后一个catch子句完成或引用该对象被销毁,直到最后std::exception_ptr.
原文:
The throw expression constructs a temporary object in unspecified storage, with the same type as expression (with cv-qualifiers removed and the type converted from array of T to pointer to T and from function returning T to pointer to function returning T, as necessary), and initializes it from expression in the same manner a function arguments or return values are initialized from the function parameters or the argument of a return expression (i.e. copy elision and move construction take place if possible). The exception object persists until the last catch clause completes or until the last std::exception_ptr that references this object is destroyed.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
一旦异常对象被构造,控制流程逆向运作(调用堆栈向上),直到它到达一个try块,在该点关联catch块的参数进行比较与抛出expression找到一个匹配的起始。如果未找到匹配项,则控制流程继续展开栈的直到下一try块,依此类推。如果找到一个匹配,控制流程跳转到匹配catch块(“异常处理程序”),将正常执行.
原文:
Once the exception object is constructed, the control flow works backwards (up the call stack) until it reaches the start of a try block, at which point the parameters of the associated catch blocks are compared with the thrown expression to find a match. If no match is found, the control flow continues to unwind the stack until the next try block, and so on. If a match is found, the control flow jumps to the matching catch block (the exception handler), which executes normally.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
“建造以来,进入相应的try块,以相反的顺序施工。如果抛出一个异常从构造函数,析构函数被称为完全非静态变量的成员和基类。这个过程被称为“堆栈展开”.
原文:
As the control flow moves up the call stack, destructors are invoked for all objects with '
自动存储时间
原文:
automatic storage duration
这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
</div>
constructed since the corresponding try-block was entered, in reverse order of construction. If an exception is thrown from a constructor, destructors are called for all fully-constructed non-static non-variant members and base classes. This process is called stack unwinding.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
2)
操作数的的投掷表达没有可能只能用在catch块(它调用了std::terminate如果使用)。放弃执行catch块,将控制权传递到下一个匹配的catch子句调用堆栈向上(但不是另一个同一个try块的catch子句后),再利用现有的异常对象:没有新的对象.....
原文:
The throw-expression without an operand may only be used inside a catch block (it calls std::terminate if used otherwise). It abandons the execution of the catch block and passes control to the next matching catch clause up the call stack (but not to another catch clause after the same try block), reusing the existing exception object: no new objects are made.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
std::terminatestd::unexpected处理的异常处理过程中产生的错误.
原文:
See std::terminate and std::unexpected for the handling of errors that arise during exception handling.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 用法

虽然投掷表达式可用于将控制转移到了执行堆栈中的任意块的代码,对于任意的原因(类似于std::longjmp),其用途是错误处理.
原文:
While throw-expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar to std::longjmp), its intended usage is error handling.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 错误处理

抛出一个异常,用来指示错误的功能,其中的“错误”通常仅限于只有以下引用错误:没有找到与</ref>对应的<ref>标签[1]:
原文:
{{{2}}}
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
#建立失败不变
原文:
# Failures to establish invariants
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
#故障,以满足后置条件
原文:
# Failures to meet the postconditions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
#故障,以满足来电者的先决条件
原文:
# Failures to meet the preconditions of the caller
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
尤其是,这意味着通过抛出异常的构造函数和大多数运营商的失败,应当报.
原文:
In particular, this implies that the failures of constructors and most operators should be reported by throwing exceptions.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 异常安全性

报告错误条件的功能,可提供额外的担保方面的程序的状态。以下四个层次的异常保证的普遍认可引用错误:没有找到与</ref>对应的<ref>标签[2][3], which are strict supersets of each other:
原文:
{{{2}}}
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
抛出(或nofail选项)异常保证“ - 函数永远不会抛出异常.
原文:
# Nothrow (or nofail) exception guarantee -- the function never throws exceptions.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
#“强异常保证” - 如果函数抛出一个异常,该程序的状态回滚到之前的状态的函数调用.
原文:
# Strong exception guarantee -- If the function throws an exception, the state of the program is rolled back to the state just before the function call.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
“基本的异常保证” - 如果函数抛出一个异常,该程序是在一个有效的状态。这可能需要清理,但所有的不变量是不变的.
原文:
# Basic exception guarantee -- If the function throws an exception, the program is in a valid state. It may require cleanup, but all invariants are intact.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
“无异常保证” - 如果函数抛出一个异常,该程序可能无法在一个有效的状态:资源泄漏,内存损坏,或其他不变毁灭的错误可能发生.....
原文:
# No exception guarantee -- If the function throws an exception, the program may not be in a valid state: resource leaks, memory corruption, or other invariant-destroying errors may have occurred.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] Exception对象

任何完整的类型和cv指向void的对象可能抛出的异常对象,所有的标准库函数抛出的匿名临时对象的值,以及这些对象的类型派生(直接或间接)从std::exception。用户自定义的异常通常遵循这个模式。引用错误:没有找到与</ref>对应的<ref>标签[4]
原文:
{{{2}}}
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
为了避免不必要的复制的异常对象和对象切片的catch子句,最好的做法是赶上参考。引用错误:没有找到与</ref>对应的<ref>标签[5][6]
原文:
{{{2}}}
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 关键字

throw

[编辑] 示例

[编辑] 参考文献

  1. H.Sutter, A. Alexandrescu (2004), "C++ Coding Standards", Item 70
  2. H. Sutter (2000) "Exceptional C++"
  3. D. Abrahams (2001) "Exception Safety in Generic Components"
  4. M. Cline, C++FAQ Lite 17.11
  5. M. Cline, C++FAQ Lite 17.12
  6. H.Sutter, A. Alexandrescu (2004) "C++ Coding Standards" Item 73
原文:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 另请参阅