この記事は翻訳作業中です。
概要
try...catch構文はtryするために命令ブロックをマークして、レスポンスを指定し、そこに例外が投げられます。
構文
try {
try_statements
}
[catch (exception_var_1 if condition_1) { // non-standard
catch_statements_1
}]
...
[catch (exception_var_2) {
catch_statements_2
}]
[finally {
finally_statements
}]
try_statements- 実行される文。
catch_statements_1,catch_statements_2- 例外が
tryブロックの中で投げられたのなら実行される文。
exception_var_1,exception_var_2- 関連付けられた
catch節に対する例外オブジェクトを保持する識別子。
condition_1- 条件式。
finally_statementstry文が完了した後に実行される文。これらの文は、例外が投げられた、または捕捉されたかどうかに関係なく実行されます。
説明
try 文は、1 つ以上の文を含む try ブロック、および少なくとも 1 つの catch 節、または finally 節、もしくはその両方から成り立っています。すなわち、try 文には 3 つの形式があります:
try...catchtry...finallytry...catch...finally
catch 節は、例外が try ブロックの中で投げられたのなら何をすべきか指定する文を含みます。すなわち、try ブロックは成功すべきで、それが成功しなかったのなら、制御は catch ブロックに渡るべきです。try ブロック内 (または try ブロック内から呼び出された関数の中) の任意の文が例外を投げたのなら、制御は即座に catch 節へ移ります。例外が try ブロックの中で投げられなかったのなら、catch 節は飛ばされます。
finally 節は、try ブロックおよび (1 つまたは複数の) catch 節が実行された後、なおかつ try 文の次の文の前に実行されます。これは、例外が投げられた、または捕捉されたかどうかに関係なく、常に実行されます。
1 つ以上の try 文をネストすることもできます。内側の try 文が catch 節を持たないのなら、囲んでいる try 文の catch 節に入ります。
Java の例外を操作するためにも try 文を使います。Java の例外に関する情報は Core JavaScript 1.5 ガイドを参照してください。
無条件 catch 節
単独の無条件 catch 節が使われたときは、任意の例外が投げられると catch 節に入ります。たとえば、次のコードの中で例外が生じたとき、制御は catch 節に移動します。
try {
throw "myException"; // 例外を生成します
}
catch (e) {
// 任意の例外を操作するための文
logMyErrors(e); // エラーハンドラに例外オブジェクトを渡します
}
条件付き catch 節
非標準
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
特定の例外を操作するために、1 つ以上の条件付き catch 節を使うこともできます。この場合、特定の例外が投げられると、適切な catch 節に入ります。次の例では、try ブロックの中のコードは 3 つの例外: TypeError、RangeError、および EvalError、を投げる可能性があります。例外が生じたとき、制御は適切な catch 節に移動します。例外が、指定された例外のどれでもなく、かつ無条件 catch 節が見つかったのなら、制御はその catch 節に移動します。
1 つ以上の条件付き catch 節とともに無条件 catch 節を使うのなら、無条件 catch 節は最後に指定されなくてはいけません。そうでなければ、無条件 catch 節が全種類の例外を、それらが条件付きのものに到達する前に、途中で捕捉します。
注: この機能は ECMAScript 仕様の一部ではありません。
try {
myroutine(); // 3 つの例外を投げる可能性があります
} catch (e if e instanceof TypeError) {
// TypeError 例外を操作するための文
} catch (e if e instanceof RangeError) {
// RangeError 例外を操作するための文
} catch (e if e instanceof EvalError) {
// EvalError 例外を操作するための文
} catch (e) {
// 任意の指定されていない例外を操作するための文
logMyErrors(e); // エラーハンドラに例外オブジェクトを渡します
}
And here is how to do implement the same "Conditional catch clauses" using only simple JavaScript conforming to the ECMAScript specification (obviously it's more verbose, but works everywhere):
try {
myroutine(); // may throw three types of exceptions
} catch (e) {
if (e instanceof TypeError) {
// statements to handle TypeError exceptions
} else if (e instanceof RangeError) {
// statements to handle RangeError exceptions
} else if (e instanceof EvalError) {
// statements to handle EvalError exceptions
} else {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
}
例外識別子
例外が try ブロックの中で投げられたときは、exception_var (たとえば、catch (e) における e) が throw 文により指定された値を保持します。投げられた例外についての情報を得るために、この識別子を使うことができます。
この識別子は catch 節にローカルです。すなわち、これは catch 節に入ったときに作成され、catch 節が実行を終了した後では、この識別子はもはや利用できません。
finally 節
finally 節は、try ブロックおよび (1 つまたは複数の) catch 節が実行された後、なおかつ try 文の次の文の前に実行される文を含みます。finally 節は、例外が投げられたかどうかに関係なく実行されます。例外が投げられたのなら、たとえ例外を操作する catch 節がなかったとしても、finally 節の中の文は実行されます。
例外が生じたときに、スクリプトを潔く失敗させるために finally 節を使うことができます。たとえば、スクリプトが結び付けていたリソースを解放する必要があるかもしれません。次の例ではファイルを開き、そのファイルを使う文を実行します (サーバーサイド JavaScript ではファイルにアクセスできます)。ファイルが開かれている間に例外が投げられたのなら、スクリプトが失敗する前に finally 節はファイルを閉じます。
openMyFile()
try {
// リソースを結び付けます
writeMyFile(theData);
}
finally {
closeMyFile(); // リソースを常に閉じます
}
Examples
Nested try-blocks
First let's see what happens with this:
try {
try {
throw new Error("oops");
}
finally {
console.log("finally");
}
}
catch (ex) {
console.error("outer", ex.message);
}
// Output:
// "finally"
// "outer" "oops"
Now, if we already caught the exception in the inner try-block by adding a catch block
try {
try {
throw new Error("oops");
}
catch (ex) {
console.error("inner", ex.message);
}
finally {
console.log("finally");
}
}
catch (ex) {
console.error("outer", ex.message);
}
// Output:
// "inner" "oops"
// "finally"
And now, lets re-throw the error.
try {
try {
throw new Error("oops");
}
catch (ex) {
console.error("inner", ex.message);
throw ex;
}
finally {
console.log("finally");
}
}
catch (ex) {
console.error("outer", ex.message);
}
// Output:
// "inner" "oops"
// "finally"
// "outer" "oops"
Any given exception will be caught only once by the nearest enclosing catch-block, unless it is re-thrown. Of course, any new exceptions raised in the "inner" block (because code in catch-block may do something that throws), will be caught by the "outer" block.
Returning from a finally block
If the finally block returns a value, this value becomes the return value of the entire try-catch-finally production, regardless of any return statements in the try and catch blocks. This includes exceptions thrown inside of the catch block:
try {
try {
throw new Error("oops");
}
catch (ex) {
console.error("inner", ex.message);
throw ex;
}
finally {
console.log("finally");
return;
}
}
catch (ex) {
console.error("outer", ex.message);
}
// Output:
// "inner" "oops"
// "finally"
The outer "oops" is not thrown because of the return in the finally block. The same would apply to any value returned from the catch block.
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 3rd Edition (ECMA-262) | 標準 | Initial definition. Implemented in JavaScript 1.4 |
| ECMAScript 5.1 (ECMA-262) try statement の定義 |
標準 | |
| ECMAScript 2015 (6th Edition, ECMA-262) try statement の定義 |
標準 | |
| ECMAScript 2017 Draft (ECMA-262) try statement の定義 |
ドラフト | Not part of the current ECMA-262 standard: Multiple catch clauses and conditional clauses (SpiderMonkey extension, JavaScript 1.5). |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (有) | (有) | (有) | (有) | (有) |
| Conditional clauses (non-standard) |
未サポート | (有) | 未サポート | 未サポート | 未サポート |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (有) | (有) | (有) | (有) | (有) | (有) |
| Conditional clauses (non-standard) |
未サポート | 未サポート | (有) | 未サポート | 未サポート | 未サポート |

