异常(或异常事件)是在执行程序期间出现的问题.当发生异常时,程序的正常流程被中断,程序/应用程序异常终止,这是不推荐的,因此要处理这些异常.
可能发生异常许多不同的原因.以下是发生异常的一些情况.
用户输入了无效数据.
无法找到需要打开的文件.
CoffeeScripts支持异常/错误处理使用尝试catch和最后块.这些块的功能与JavaScript中的相同, try 块保存异常语句, catch 块具有发生异常时要执行的操作,并且 finally 块用于无条件执行语句.
以下是尝试捕获和最终的语法CoffeeScript中的块.
try // Code to run catch ( e ) // Code to run if an exception occurs finally // Code that is always executed regardless of // an exception occurring
尝试块必须紧跟一个 catch 块或一个 finally 块(或两者中的一个).当 try 块中发生异常时,异常将放在 e 中,并执行 catch 块.在try/catch之后无条件执行可选的 finally 块.
以下示例演示使用try进行异常处理并在CoffeeScript中捕获块.在这里,我们尝试在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
我们也可以使用的文件中.
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
CoffeeScript还支持 throw 语句.您可以使用throw语句来引发内置异常或自定义异常.稍后可以捕获这些异常并且您可以采取适当的操作.
以下示例演示了 throw
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.