throw expression
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
信号错误的条件和执行错误处理程序.
原文:
Signals an erroneous condition and executes an error handler.
目录 |
[编辑] 语法
throw expression
|
(1) | ||||||||
throw
|
(2) | ||||||||
[编辑] 解释
- “尝试”和“捕捞”(异常处理程序)模块的更多信息,请参阅<div class="t-tr-text"> try-catch块原文:try-catch block这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
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.一旦异常对象被构造,控制流程逆向运作(调用堆栈向上),直到它到达一个
“建造以来,进入相应的try块,以相反的顺序施工。如果抛出一个异常从构造函数,析构函数被称为完全非静态变量的成员和基类。这个过程被称为“堆栈展开”.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.原文:
As the control flow moves up the call stack, destructors are invoked for all objects with '
自动存储时间</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.
原文:
automatic storage duration
这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
操作数的的投掷表达没有可能只能用在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.
见std::terminatestd::unexpected处理的异常处理过程中产生的错误.
原文:
See std::terminate and std::unexpected for the handling of errors that arise during exception handling.
[编辑] 用法
虽然投掷表达式可用于将控制转移到了执行堆栈中的任意块的代码,对于任意的原因(类似于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.
[编辑] 错误处理
#建立失败不变
#故障,以满足后置条件
原文:
# Failures to meet the postconditions
#故障,以满足来电者的先决条件
原文:
# Failures to meet the preconditions of the caller
尤其是,这意味着通过抛出异常的构造函数和大多数运营商的失败,应当报.
原文:
In particular, this implies that the failures of constructors and most operators should be reported by throwing exceptions.
[编辑] 异常安全性
报告错误条件的功能,可提供额外的担保方面的程序的状态。以下四个层次的异常保证的普遍认可引用错误:没有找到与
</ref>
对应的<ref>
标签[2][3], which are strict supersets of each other:#抛出(或nofail选项)异常保证“ - 函数永远不会抛出异常.
原文:
# Nothrow (or nofail) exception guarantee -- the function never throws exceptions.
#“强异常保证” - 如果函数抛出一个异常,该程序的状态回滚到之前的状态的函数调用.
原文:
# 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.
“基本的异常保证” - 如果函数抛出一个异常,该程序是在一个有效的状态。这可能需要清理,但所有的不变量是不变的.
原文:
# 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.
“无异常保证” - 如果函数抛出一个异常,该程序可能无法在一个有效的状态:资源泄漏,内存损坏,或其他不变毁灭的错误可能发生.....
原文:
# 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.
[编辑] Exception对象
任何完整的类型和cv指向void的对象可能抛出的异常对象,所有的标准库函数抛出的匿名临时对象的值,以及这些对象的类型派生(直接或间接)从std::exception。用户自定义的异常通常遵循这个模式。引用错误:没有找到与
</ref>
对应的<ref>
标签[4][编辑] 关键字
[编辑] 示例
本章尚未完成 |
[编辑] 参考文献
- ↑ H.Sutter, A. Alexandrescu (2004), "C++ Coding Standards", Item 70
- ↑ H. Sutter (2000) "Exceptional C++"
- ↑ D. Abrahams (2001) "Exception Safety in Generic Components"
- ↑ M. Cline, C++FAQ Lite 17.11
- ↑ M. Cline, C++FAQ Lite 17.12
- ↑ H.Sutter, A. Alexandrescu (2004) "C++ Coding Standards" Item 73
原文:
[编辑] 另请参阅
- <div class="t-tr-text"> try-catch块</div>原文:try-catch block这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。 - <div class="t-tr-text"> noexcept说明</div>原文:noexcept specifier这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。 - <div class="t-tr-text"> 异常规范</div>原文:exception specifications这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。