我正在尝试捕获 node.js uncaughtException 的堆栈跟踪,它对于不同的错误都可以正常工作,但对于 throw() 语句却不起作用:
异常处理的正确堆栈跟踪:
$ cat errorFunc.js
process.on('uncaughtException', function(exception) {
console.log('uncaughtException occurred: ' + exception.stack);
});
MyError();
$ node errorFunc.js
uncaughtException occurred: ReferenceError: MyError is not defined
at Object.<anonymous> (/home/jolcese/code/WebEnclaves/Server/errorFunc.js:5:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
$
缺少由 throw() 引起的异常的堆栈跟踪:
$ cat errorThrow.js
process.on('uncaughtException', function(exception) {
console.log('uncaughtException occurred: ' + exception.stack);
});
throw('my error');
$ node errorThrow.js
uncaughtException occurred: undefined
$
知道为什么吗?
谢谢
何塞
免责声明:我知道使用 process.on('uncaughtException') 是一件非常非常糟糕的事情,我会受到惩罚,但在此代码中不可以选择使用域。
JavaScript 可以让你抛出任何东西。
如果你想在 JavaScript 中抛出堆栈跟踪错误,你需要抛出Error
对象。(规格 )
另外, throw 是一个运算符,而不是一个函数。
Try
throw new Error('my error');
See 手册请访问 Mozilla 开发者网络以获取更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)