📜  CoffeeScript-异常处理

📅  最后修改于: 2020-10-26 05:50:41             🧑  作者: Mango


异常(或例外事件)是在程序执行期间出现的问题。当发生异常时,程序的正常流程将中断,并且程序/应用程序将异常终止,因此不建议这样做,因此应处理这些异常。

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

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

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. (C:\Examples\strings_exceptions\Exception_handling.coffee:3:7)
  at Object. (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. (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块重写上面的示例。如果这样做,则在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. (C:\Examples\strings_exceptions\Exception_handling.coffee:3:7)
  at Object. (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. (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语句引发内置异常或自定义异常。以后可以捕获这些异常,您可以采取适当的措施。

下面的示例演示了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.