try-catch statement
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
用来试图执行的化合物陈述,同时捕捉和处理可能已作为这种尝试的结果抛出的异常.
原文:
Used to attempt the execution of a compound-statement, while catching and handling exceptions that may have been thrown as a result of this attempt.
目录 |
[编辑] 语法
try { statements } catch ( exception-decl ) { statements }
|
(1) | ||||||||
try { statements } catch ( exception-decl-1 ) { statements } catch ( exception-decl-2 ) { statements }
|
(2) | ||||||||
try { statements } catch ( exception-decl ) { statements throw; }
|
(3) | ||||||||
try { statements } catch ( ... ) { statements }
|
(4) | ||||||||
try : ctor-init-list { statements } catch ( exception-decl ) { statements }
|
(5) | ||||||||
[编辑] 解释
基本语法(1)显示一个try-catch块的基本组成部分。首先,关键词
try
启动statements在其中可以执行一个try块。需要注意的是在try块,开的花括号的收盘,遵循相同的作用域规则的任何其他范围(例如,变量的try块内声明的是不是在它之外,包括从catch块, )。然后,catch
语句作为被捕获,如果从在try块抛出的异常的类型的说明符。 exception-decl有相同的语法,参数声明的类型在一个单参数的函数声明(除不能void,一个不完整的类型,或右值引用(C++11 起))。最后,复合语句 {
statements }
后面的catch语句被称为“异常处理”,并包含在被抓的异常语句的执行。典型的异常处理代码,包括记录错误,采用了另一种方法,试图在try块,或重新包装的异常抛出的异常的附加信息到另一个.原文:
The basic syntax (1) shows the basic components of a try-catch block. First, the keyword
try
initiates a try-block in which statements can be executed. Note that the try-block, from the opening curly-brace to its closing, follows the same scoping rules as any other scope (e.g. variables declared within the try-block are not available outside of it, including from the catch-block that follows). Then, the catch
statement acts as a specifier of the type of exception to be caught if thrown from within the try-block. The exception-decl has the same syntax as the parameter declaration within a single-parameter function declaration (except that the type cannot by void, an incomplete type, or an rvalue-reference (C++11 起)). Finally, the compound-statement {
statements }
which follow the catch-statement is called the exception-handler and contains statements to be executed in response to the exception that was caught. Typical exception-handling code includes logging the error, employing an alternative method to what was attempted in the try-block, or re-packaging the exception into another thrown exception with additional information.在语法(2),说明这里是一个try-catch块不局限于单一的catch块。由于不同类型的异常被抛出try块内,可以指定为必要的catch块来处理所有的异常处理的一个愿望。但是,请注意,那顺序的的追赶报表的外观是非常重要的,抛出的异常将被处理的第一个catch块,按顺序的外观,其exception-decl是一个有效的匹配(和隐式转换也同样适用)。换句话说,它是“不”的“最佳”匹配的选择(函数重载规则),但“第一”的比赛。如果抛出的异常匹配没有追赶报表,然后异常运回,直到另一包围的try块达到或由于未处理的异常,直到程序终止.
原文:
In syntax (2), the illustration here is that a try-catch block is not limited to a single catch-block. Since different types of exceptions can be thrown from within the try-block, one can specify as many catch-blocks as necessary to handle all exceptions one wishes to handle. Note, however, that the order of appearance of the catch-statements is important, a thrown exception will be handled by the first catch-block, by order of appearance, whose exception-decl is a valid match (and implicit conversions apply as well). In other words, it is not the best match that is chosen (as in function overloading rules), but the first match. If the thrown exception matches none of the catch-statements, then the exception is carried back until another enclosing try-block is reached or until the program is terminated due to an unhandled exception.
(3)在语法中,唯一增加的是
throw;
在catch语句块。该语句的重新抛出相同的异常对象的catch块捕获的效果。这是唯一的一个空掷语句上下文中是有效的,它的具体含义是重新抛出异常,被抓住了,并宣布为exception-decl.原文:
In syntax (3), the only addition is the
throw;
statement within the catch-block. The statement has the effect of re-throwing the same exception object which was caught by the catch-block. This is the only context in which an empty throw-statement is valid, and it's specific meaning is to re-throw the exception that was caught, and declared as exception-decl.(4)语法是所谓的“赶超所有块”。 “省略号运营商”
的类,它是通常更为有用捕获该异常,如catch( std::exception& e )。然而,一个catch-all块的主要目的是为了确保没有未捕获的异常被泄露的功能,这是特别有用的特殊功能泄漏例外是危险的,最特别的是,析构函数或一个动态链接外部功能....
(从字面上看,三个点),可以使用在exception-decl指定的任何和所有类型的异常应该被捕获的catch块。得到任何信息被抛出的异常的类型,这种类型的捕获所有块是没有用的,因为大多数的异常对象是来自<div class="t-tr-text"> std :: exception的原文:
std::exception
这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
原文:
Syntax (4) is a so-called catch-all block. The ellipsis operator
...
(literally, three dots) can be used in-place of the exception-decl to specify that any and all types of exceptions should be caught by the catch-block. This type of catch-all block is not useful for getting any information on the type of exception that was thrown, and since most exception objects are of classes derived from std :: exception的</div>, it is generally more useful to catch the exception as catch( std::exception& e ). However, the main purpose of a catch-all block is to ensure that no uncaught exceptions are leaked from a function, which is especially useful for special functions from which leaking exceptions can be dangerous, most notably, a destructor or a dynamically-linked external function.
原文:
std::exception
这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
语法(5)被称为“函数try块”,并且可以用于try块内以包围整个函数体(与catch块跟随它)。捕获异常的构造函数的初始化列表中的子类的对象(建筑),其实在执行过程中可能会被抛出,这是非常有用的,它这样做是唯一的出路。这“功能的try块”语法在任何其他情况下很少使用,因为它没有任何优势,相比传统的try-catch块,产生的语法一般是没有吸引力(和不熟悉的大部分).
原文:
Syntax (5) is called a function-try-block and can be used to enclose an entire function body inside a try-block (with catch-blocks following it). This is especially useful to catch exceptions which could be thrown during the execution of a constructor's initialization list (construction of sub-objects of a class), in fact, it is the only way to do so. This function-try-block syntax is seldom used in any other context because it has no advantage as compared to a traditional try-catch block, and the resulting syntax is generally unappealing (and unfamiliar to most).
[编辑] 关键字
[编辑] 示例
下面的例子演示了几种在某些情况下的
try-catch
块
原文:
The following example demonstrates several usage cases of the
try-catch
block
#include <iostream> #include <vector> int main() { try { std::cout << "Throwing an integer exception...\n"; throw int(42); } catch( int i ) { std::cout << " the integer exception was caught, with value: " << i << '\n'; } try { std::cout << "Creating a vector of size 5... \n"; std::vector<int> v(5); std::cout << "Accessing the 11th element of the vector...\n"; v.at(10); // the at() function will check the range. } catch( std::exception& e) { std::cout << " a standard exception was caught, with message '" << e.what() << "'\n"; } }
输出:
Throwing an integer exception... the integer exception was caught, with value: 42 Creating a vector of size 5... Accessing the 11th element of the vector... a standard exception was caught, with message 'out_of_range'