Add global try/catch to JavaScript

后端 未结 2 724
我寻月下人不归
我寻月下人不归 2020-12-14 07:37

Is there some way to wrap the entire page in a try/catch, so that I can catch any error from any script that is executing?

相关标签:
2条回答
  • 2020-12-14 08:09

    Turn on the debugger and break on error.

    You don't want to catch every error in the entire page on production code. That's horrible.

    0 讨论(0)
  • 2020-12-14 08:12

    Use window.onerror instead of a big try/catch.

    You could actually do some useful things in the error handler, like posting the error information to your server so you know when things are breaking on your page.

    0 讨论(0)
提交回复
热议问题