【发布时间】:2013-05-23 16:17:09
【问题描述】:
我正在尝试捕获 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 node.js