Is it possible to catch exceptions thrown in a JavaScript async callback?

前端 未结 7 1227
春和景丽
春和景丽 2020-11-27 04:15

Is there a way to catch exceptions in JavaScript callbacks? Is it even possible?

Uncaught Error: Invalid value for property 

7条回答
  •  伪装坚强ぢ
    2020-11-27 04:45

    Yes, you can override the default behaviour of window.onerror:

    window.onerror = function(message, file, lineNumber) {
      // all errors will be caught here
      // you can use `message` to make sure it's the error you're looking for
      // returning true overrides the default window behaviour
      return true; 
    };
    

提交回复
热议问题