是否有办法知道某些代码是否在try catch块下? (特别是在节点中)
我想做的是这样的(使用变量bungleInTry
来模拟我要寻找的东西)
let acumulateErrors=[];
let bungleInTry=0; // This to be removed, it's to simulate the expected behavior
function assert(cond,msg){
if (!cond){
if (underTryCatch())
throw(Error(msg)); // Somewhere somebody is in control
else
acumulateErrors.push(msg); // I don't want a crash but I have to keep this
}
}
assert(1==2, "1 doesn't equals 2");
bungleInTry++; // (simulating what I am asking)
try{
assert(3==4, "3 doesn't equals 4");
}catch(error){
console.log(error.message);
}
bungleInTry--; // to be removed
console.log(acumulateErrors);
function underTryCatch(){
return bungleInTry;
}
答案 0 :(得分:0)
否,无法检测到将捕获引发的错误,而该信息仅对您的代码不可用。 (并且可以说,所有错误都会被捕获,即使它们是由Node自己的错误处理程序捕获的,该错误处理程序会将其转储到控制台并终止进程... :-))