Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js中有哪些未捕获的异常?_Node.js - Fatal编程技术网

Node.js中有哪些未捕获的异常?

Node.js中有哪些未捕获的异常?,node.js,Node.js,Node.js中的未捕获异常(以及一般异常)是什么? 所有的资源都是关于如何处理它们的,但我没有找到任何解释它们是什么以及它们为什么会发生。一个例外基本上是当某些东西“断裂”时。例如: alert(x); var array = ["A", "B", "C"]; var s = array[1357].toLowerCase(); // TypeError: Cannot read property 'toLowerCase' of undefined someOther.code().to

Node.js中的未捕获异常(以及一般异常)是什么?


所有的资源都是关于如何处理它们的,但我没有找到任何解释它们是什么以及它们为什么会发生。

一个例外基本上是当某些东西“断裂”时。例如:

alert(x);
var array = ["A", "B", "C"];
var s = array[1357].toLowerCase();
// TypeError: Cannot read property 'toLowerCase' of undefined

someOther.code().toRun(); // this will NOT run, execution is aborted at the exception
将导致“ReferenceError:x未定义”,因为x尚未定义。这是一个意外的例外

处理异常的一种方法是将它们包装在一个简单的try/catch中:

try { 
  alert(x)
}
catch (e) {
  alert("x wasn't defined");
}
为了保持代码平稳运行,您需要尝试捕获并处理所有潜在的异常,否则脚本将停止处理


阅读更多有关

的信息,当代码执行可能不应该执行的操作时,会发生异常。对于所有的事情,都有大量的例外情况

例如:

alert(x);
var array = ["A", "B", "C"];
var s = array[1357].toLowerCase();
// TypeError: Cannot read property 'toLowerCase' of undefined

someOther.code().toRun(); // this will NOT run, execution is aborted at the exception
这是个例外

Uncaught仅仅意味着没有任何代码在寻找该执行选项,以便能够优雅地处理它。未捕获的异常将停止代码的执行,并在控制台中显示为错误。在生产代码中,未捕获的异常是一件非常糟糕的事情

使用try/catch块捕获未捕获的异常。你可能在所有你找到的“如何处理它们”资源中都读到了

try {
  var array = ["A", "B", "C"];
  var s = array[1357].toLowerCase();
} catch (e) {
  console.log("Don't do that, seriously");
}
someOther.code().toRun(); // this does run, execution continues after caught exception