window.onerror 并尝试捕获 js

window.onerror and try catch js

我在我的全局项目中定义了一个 window.onerror 我还有一个抛出错误的方法 我试图在 try catch 块中捕获错误,但由于某种原因它没有在 catch 子句处停止

window.onerror = (...) => {...}
const throwError = () => {throw new Error("")} 
try { throwError() } catch (e) { ... }

正如伯格比所说 -

My crystal ball says that you're throwing the error in an asynchronous callback. Yes, the problem is deeper than the snippet you posted here, please edit your question to include code that actually reproduces the unexpected behaviour. – Bergi Feb 15 at 12:16

他是对的,这就是问题所在——我可以通过更改异步回调来解决这个问题