简体   繁体   English

如何判断代码是否在try catch块下

[英]How to tell if code is under a try catch block

Is there a way to know if the some code is under a try catch block? 有没有办法知道某些代码是否在try catch块下? (specifically in node) (特别是在节点中)

What I want to do is something like this (using a variable bungleInTry to simulate what I am looking for) 我想要做的就是这样(使用变量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; } 

No, there's no way of detecting that an error thrown will be caught, that information just isn't available to your code. 不,无法检测到将捕获抛出的错误,该信息对您的代码不可用。 (And arguably, all errors are caught, even if they're caught by Node's own error handler that dumps them out to the console and terminates the process... :-) ) (可以说,所有错误都会被捕获,即使它们是由Node自己的错误处理程序捕获的,该错误处理程序会将它们转储到控制台并终止进程... :-))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM