- CoffeeScript 教程
- CoffeeScript - 首頁
- CoffeeScript - 概述
- CoffeeScript - 環境
- CoffeeScript - 命令列工具
- CoffeeScript - 語法
- CoffeeScript - 資料型別
- CoffeeScript - 變數
- CoffeeScript - 運算子和別名
- CoffeeScript - 條件語句
- CoffeeScript - 迴圈
- CoffeeScript - 推導式
- CoffeeScript - 函式
- CoffeeScript 面向物件
- CoffeeScript - 字串
- CoffeeScript - 陣列
- CoffeeScript - 物件
- CoffeeScript - 範圍
- CoffeeScript - 展開運算子
- CoffeeScript - 日期
- CoffeeScript - 數學
- CoffeeScript - 異常處理
- CoffeeScript - 正則表示式
- CoffeeScript - 類和繼承
- CoffeeScript 高階
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 有用資源
- CoffeeScript - 快速指南
- CoffeeScript - 有用資源
- CoffeeScript - 討論
CoffeeScript - 異常處理
異常(或異常事件)是在程式執行期間出現的錯誤。當發生異常時,程式的正常流程會被中斷,程式/應用程式會異常終止,這並不推薦,因此需要處理這些異常。
異常可能由於許多不同的原因發生。以下是一些發生異常的情況。
- 使用者輸入了無效資料。
- 需要開啟的檔案找不到。
CoffeeScript 中的異常
CoffeeScript 使用**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**塊後面必須緊跟一個**catch**塊或一個**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\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:134:23) at compileScript (C:\Users\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:224:29) at compilePath (C:\Users\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:174:14) at Object.exports.run (C:\Users\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:98:20) at Object.<anonymous> (C:\Users\Tutorialspoint\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 塊
我們也可以使用**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\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:134:23) at compileScript (C:\Users\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:224:29) at compilePath (C:\Users\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:174:14) at Object.exports.run (C:\Users\Tutorialspoint\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:98:20) at Object.<anonymous> (C:\Users\Tutorialspoint\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.