CoffeeScript教程

CoffeeScript 异常处理

异常(或异常事件)是在程序执行过程中出现的问题。当异常发生时,程序的正常流程被打乱,程序/应用程序异常终止,不推荐,因此需要处理这些异常。
出现异常的原因有很多。下面是一些发生异常的场景。
用户输入了无效数据。 找不到需要打开的文件。

CoffeeScript 中的异常

CoffeeScripts 支持使用 try catch 和 finally 块处理异常/错误。这些块的功能与 JavaScript 相同, try 块包含异常语句, catch 块具有发生异常时要执行的操作, finally 块用于无条件地执行语句。
以下是 CoffeeScript 中 try catchfinally 块的语法。
 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 操作中使用未定义的符号,并使用 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
打开 命令提示符并编译.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 块重写上面的例子。如果我们这样做,这个块的内容会在 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"
打开 命令提示符并编译.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.
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4