Coffeescript-exception-handling
CoffeeScript-例外処理
例外(または例外的なイベント)は、プログラムの実行中に発生する問題です。 例外が発生すると、プログラムの通常のフローが中断され、プログラム/アプリケーションが異常終了します。これは推奨されません。したがって、これらの例外は処理されます。
例外は、さまざまな理由で発生する可能性があります。 例外が発生するいくつかのシナリオを次に示します。
- ユーザーが無効なデータを入力しました。
- 開く必要があるファイルが見つかりません。
CoffeeScriptの例外
CoffeeScriptsは、 try catchブロックとfinally ブロックを使用した例外/エラー処理をサポートしています。 これらのブロックの機能はJavaScriptと同じです。 try ブロックには例外ステートメントが保持され、 catch ブロックには例外が発生したときに実行されるアクションがあり、 finally ブロックはステートメントを無条件に実行するために使用されます。
以下は、CoffeeScriptの try catch および finally ブロックの構文です。
try
//Code to run
catch ( e )
//Code to run if an exception occurs
finally
//Code that is always executed regardless of
//an exception occurring
*try* ブロックの後には、正確に1つの *catch* ブロックまたは1つの *finally* ブロック(または両方)が続く必要があります。 *try* ブロックで例外が発生すると、例外は *e* に配置され、 *catch* ブロックが実行されます。 オプションの *finally* ブロックは、try/catchの後に無条件に実行されます。
例
次の例は、CoffeeScriptでtryブロックとcatchブロックを使用した例外処理を示しています。 ここでは、CoffeeScript操作で未定義のシンボルを使用しようとしており、 try および catch ブロックを使用して発生したエラーを処理しました。 このコードを Exception_handling.coffee という名前のファイルに保存します
try
x = y+20
console.log "The value of x is :" +x
catch e
console.log "exception/error occurred"
console.log "The STACKTRACE for the exception/error occurred is ::"
console.log e.stack
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c Exception_handling.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var e, error, x;
try {
x = y + 20;
console.log("The value of x is :" + x);
} catch (error) {
e = error;
console.log("exception/error occurred");
console.log("The STACKTRACE for the exception/error occurred is ::");
console.log(e.stack);
}
}).call(this);
次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。
c:\> coffee Exception_handling.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
exception/error occurred
The STACKTRACE for the exception/error occurred is ::
ReferenceError: y is not defined
at Object.<anonymous> (C:\Examples\strings_exceptions\Exception_handling.coffee:3:7)
at Object.<anonymous> (C:\Examples\strings_exceptions\Exception_handling.coffee:2:1)
at Module._compile (module.js:413:34)
at Object.exports.run (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:134:23)
at compileScript (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:224:29)
at compilePath (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:174:14)
at Object.exports.run (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:98:20)
at Object.<anonymous> (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\bin\coffee:7:41)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
最終ブロック
*finally* ブロックを使用して、上記の例を書き換えることもできます。 そうすると、このブロックの内容は *try* と *catch* の後に無条件に実行されます。 このコードを *Exception_handling_finally.coffee* という名前のファイルに保存します
try
x = y+20
console.log "The value of x is :" +x
catch e
console.log "exception/error occurred"
console.log "The STACKTRACE for the exception/error occurred is ::"
console.log e.stack
finally
console.log "This is the statement of finally block"
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c Exception_handling_finally.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var e, error, x;
try {
x = y + 20;
console.log("The value of x is :" + x);
} catch (error) {
e = error;
console.log("exception/error occurred");
console.log("The STACKTRACE for the exception/error occurred is ::");
console.log(e.stack);
} finally {
console.log("This is the statement of finally block");
}
}).call(this);
次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。
c:\> coffee Exception_handling_finally.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
exception/error occurred
The STACKTRACE for the exception/error occurred is ::
ReferenceError: y is not defined
at Object.<anonymous> (C:\Examples\strings_exceptions\Exception_handling.coffee:3:7)
at Object.<anonymous> (C:\Examples\strings_exceptions\Exception_handling.coffee:2:1)
at Module._compile (module.js:413:34)
at Object.exports.run (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:134:23)
at compileScript (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:224:29)
at compilePath (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:174:14)
at Object.exports.run (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:98:20)
at Object.<anonymous> (C:\Users\finddevguides\AppData\Roaming\npm\node_modules\coffee-script\bin\coffee:7:41)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
This is the statement of finally block
throwステートメント
CoffeeScriptは throw ステートメントもサポートしています。 throwステートメントを使用して、組み込み例外またはカスタマイズされた例外を発生させることができます。 後でこれらの例外をキャプチャして、適切なアクションを実行できます。
例
次の例は、CoffeeScriptでの throw ステートメントの使用法を示しています。 このコードを throw_example.coffee という名前のファイルに保存します
myFunc = ->
a = 100
b = 0
try
if b == 0
throw ("Divided by zero error.")
else
c = a/b
catch e
console.log "Error: " + e
myFunc()
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c throw_example.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var myFunc;
myFunc = function() {
var a, b, c, e, error;
a = 100;
b = 0;
try {
if (b === 0) {
throw "Divided by zero error.";
} else {
return c = a/b;
}
} catch (error) {
e = error;
return console.log("Error: " + e);
}
};
myFunc();
}).call(this);
次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。
c:\> coffee throw_example.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
Divided by zero error.