我正在寻找一种解决方案,在该解决方案中,我可以捕获浏览器控制台中记录的所有错误(已处理/未处理)。JavaScript Capture处理错误
我知道关于window.onerror
和window.addeventlistener('error', function(){})
。
上述代码仅捕获未处理的错误。我还需要捕获处理的错误。
例如:
function foo() {
var x = null;
try {
x.a = "";
} catch (e) {
//Exception will be digested here.
}
var y = null
y.b = "";
}
window.onerror = function() {
//Write logic for the errors logged in console.
}
foo();
在上面的例子try catch
是存在的,所以我会得到错误仅适用于可变y
不x
。
是否有任何方法来聆听/捕获catch
块?
谢谢
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch#The_finally_clause –
你可以这样做,当使用调试器。但不是来自JS本身。你为什么想这样做? – Oriol
如果你真的想要,你可以抛出一个自定义错误后,将其传递给window.onerror。例如:https://fiddle.jshell.net/L29jv5fv/你想做什么? –