try-catch statement
提供: cppreference.com
|
|
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
この試みの結果としてスローされている可能性があり、例外をキャッチして処理している間、複合文の実行を試みるために使われる.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集] 構文
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) | ||||||||
[編集] 説明
- を参照してくださいOriginal:See例外をスローします</div> for more information about throw exceptionsOriginal:throw exceptionsThe text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
基本構文(1)は、try-catchブロックの基本的なコンポーネントを示しています。まず、キーワード
tryはstatementsを実行できるのtryブロックを開始します。開中括弧からその閉鎖しようとするブロックは、他の任意の範囲(例えば、変数はtryブロック内で宣言されたそれの外では利用できません、次のcatchブロックからのものを含むと同じスコープ規則に従っていることに注意してください)。その後、catch文はtryブロック内から送出された場合にキャッチする例外の型の指定子として機能します。 exception-declは、単一パラメータ関数宣言内のパラメータの宣言と同じ構文を持っています(ただし、入力することができませんvoidによって、不完全な型、または右辺値参照(C++11およびそれ以降))。最後に、複合文 { statements } キャッチ文はは例外ハンドラと呼ばれて従うと、逮捕された例外に応答して実行するステートメントが含まれている。典型的な例外処理コードは、追加情報を使用して別のスローされた例外に例外をtryブロックにしようとしたか、再梱包されたものに代わる方法を採用すると、エラーをログに記録含む.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
構文(2)において、ここにイラストは、try-catchブロックは単一のcatchブロックに限定されるものではないということです。例外は、異なる種類のtryブロック内からスローされる可能性があるので、1つはすべての例外を処理する1つの願いを処理するために、できるだけ多くの必要に応じてキャッチブロックを指定することができます。ただし、キャッチ文の出現順序が重要であることを、スローされた例外がexception-decl有効な一致(および暗黙的な変換が同様に適用される)である外観、の順序によって、最初のcatchブロックで処理されます。言い換えれば、それは(ルールを関数のオーバーロードのように)が選ばれているありません店頭'最高の試合が、'第一試合です。スローされた例外がキャッチステートメントにも一致しない場合は、例外が別のtryブロックに到達するか、プログラムが処理されない例外のために終了されるまで同封するまで戻って行われ.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
構文(3)において、添加のみではキャッチブロック内
throw;文です。ステートメントは、catchブロックでキャッチされた例外オブジェクトを再スローするという効果があります。これは空のスローステートメントが有効となっているコンテキストのみであり、それの具体的な意味は捕え、exception-declとして宣言された例外を再スローすることです.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
構文(4)は、いわゆるキャッチオールブロックです。 の省略記号演算子
から派生したクラスであるので、それはcatch( std::exception& e )として例外をキャッチするために、一般の方が便利です。しかし、キャッチオールブロックの主な目的は、キャッチされない例外が例外をリークすると、最も顕著なのは、デストラクタまたは動的にリンクされた外部の危険なことができ、そこから特殊な機能のために特に有用である関数から漏洩していないことを確認することです機能....(文字通り、3ドット)の例外のいずれか、すべてのタイプがcatchブロックでキャッチされるべきであることを指定するexception-decl別の場所で使用することができます。キャッチオールブロックのこのタイプは、スローされた例外の種類に任意の情報を入手するために有効ではなく、ほとんどの例外オブジェクトは<div class="t-tr-text"> のstd ::例外Original:
std::exception
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
Original:
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 ::例外</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.
Original:
std::exception
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
構文(5)関数tryブロックと呼ばれると(それに続くキャッチブロックで)tryブロック内部関数の本文全体を囲むために使用することができます。これは、コンストラクタの初期化リストの実行(クラスのサブオブジェクトの構築)中にスローされる可能性のある例外をキャッチするのに特に有用である、実際には、それはそうする唯一の方法です。従来のtry-catchブロックに比べて、それは何の利点もありませんし、その結果の構文は一般的に魅力のない(そしてほとんどのに慣れていない)であるため、この関数tryブロック構文はめったに他のコンテキストで使用されていません.
Original:
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).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] キーワード
[編集] 例
次の例では、
try-catchブロックのいくつかの使用例を示しています
Original:
The following example demonstrates several usage cases of the
try-catch block
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Run this code
#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'

