目录

CoffeeScript - 异常处理( Exception Handling)

异常(或异常事件)是在执行程序期间出现的问题。 发生异常时,程序的正常流程中断,程序/应用程序异常终止,这是不推荐的,因此要处理这些异常。

出于许多不同的原因可能会发生异常。 以下是发生异常的一些情况。

  • 用户输入了无效数据。
  • 找不到需要打开的文件。

CoffeeScript中的例外情况

CoffeeScripts使用try catch and finally块支持异常/错误处理。 这些块的功能与JavaScript中的相同, try块保存异常语句, catch块具有在发生异常时要执行的操作, finally块用于无条件地执行语句。

以下是Try 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

try块必须紧跟一个catch块或一个finally块(或两者之一)。 当try块中发生异常时,异常放在e并执行catch块。 在try/catch之后无条件执行可选的finally块。

例子 (Example)

以下示例演示了在CoffeeScript中使用try和catch块进行异常处理。 在这里,我们尝试在CoffeeScript操作中使用未定义的符号,并使用trycatch块处理发生的错误。 将此代码保存在名为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

打开command prompt并编译.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);

现在,再次打开command prompt并运行CoffeeScript文件,如下所示。

c:\> coffee Exception_handling.coffee

执行时,CoffeeScript文件生成以下输出。

<b class="notranslate">exception/error occurred
The STACKTRACE for the exception/error occurred is :: </b>
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\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:134:23)
  at compileScript (C:\Users\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:224:29)
  at compilePath (C:\Users\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:174:14)
  at Object.exports.run (C:\Users\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:98:20)
  at Object.<anonymous> (C:\Users\IoWiki\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块重写上面的例子。 如果我们这样做,则在trycatch之后无条件地执行该块的内容。 将此代码保存在名为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"

打开command prompt并编译.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);

现在,再次打开command prompt并运行CoffeeScript文件,如下所示。

c:\> coffee Exception_handling_finally.coffee

执行时,CoffeeScript文件生成以下输出。

<b class="notranslate">exception/error occurred
The STACKTRACE for the exception/error occurred is :: </b>
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\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:134:23)
  at compileScript (C:\Users\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:224:29)
  at compilePath (C:\Users\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:174:14)
  at Object.exports.run (C:\Users\IoWiki\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:98:20)
  at Object.<anonymous> (C:\Users\IoWiki\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语句来引发内置异常或自定义异常。 稍后可以捕获这些异常,您可以采取适当的措施。

例子 (Example)

以下示例演示了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()

打开command prompt并编译.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); 

现在,再次打开command prompt并运行CoffeeScript文件,如下所示。

c:\> coffee throw_example.coffee

执行时,CoffeeScript文件生成以下输出。

Divided by zero error.
↑回到顶部↑
WIKI教程 @2018