JavaScript 错误和异常处理程序

发布于 2024-10-07 18:55:15 字数 97 浏览 0 评论 0原文

是否可以注册一个错误或异常处理程序/函数,当 javascript 错误或异常发生时将执行该处理程序/函数?我只是觉得将所有代码包装在 try/catch 块中似乎非常乏味且低效。

is it possible to register an error or exception handler/function that will be executed when a javascript error or exception occurs? I just feel wrapping all codes in try/catch block seems very tedious and inefficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷雾森÷林ヴ 2024-10-14 18:55:15
window.onerror = function (msg, file, line) {
    // handle error here
}

支持:

  • Chrome 13+
  • Firefox 6.0+
  • Internet Explorer 5.5+
  • Opera 11.60+
  • Safari 5.1+
window.onerror = function (msg, file, line) {
    // handle error here
}

Supported by:

  • Chrome 13+
  • Firefox 6.0+
  • Internet Explorer 5.5+
  • Opera 11.60+
  • Safari 5.1+
我不吻晚风 2024-10-14 18:55:15

Andy E 的回答 (+1) 告诉您如何做到这一点。

也就是说,JavaScript 并不真正意味着像 Java 那样捕获异常。如果您的代码引发异常,请调出控制台并使用调试器来修复它们。 JS 异常很慢,而且实际上不适合用于流量控制。除非出现严重问题,否则方法不会抛出异常 - 通常是编程错误。

Andy E's answer (+1) tells you how to do it.

That said, JavaScript isn't really meant to have caught execeptions in the same sense that, say, Java does. If your code is throwing exceptions, pull up a console and use the debugger to fix them. JS exceptions are slow, and really not meant to be used for flow control. A method won't throw an exception unless there's a serious problem — and it's usually a programming error.

究竟谁懂我的在乎 2024-10-14 18:55:15

这是 window.onerror 解决方案的替代答案。这不是我在生产中使用过的东西,但我只是喜欢它,因为它具有灵活性(即您可以使用它来调试诸如计时方法花费的时间之类的事情)。

虽然您可能可以将 window 传递给它(不要引用我的话,并且认为这不是一个好主意),但如果您拥有所有 对象中的方法

(function(obj) {

    for (var name in obj) {
        if (typeof(obj[name]) == 'function') {
            currentMethod = obj[name];
            obj[name] = function() {
                try {
                    currentMethod();
                }
                catch (e) {
                    alert('Exception Handler: ' + e);
                }
            };
        }
    }

}(myObject));

它的工作原理:http://jsfiddle.net/jonathon/kpYnW/

基本上,它会遍历对象中的每个属性,找到函数并包装它们在 try/catch 中(或者任何你想要的)。

它是否有效是另一回事 - 我刚刚发现它是一种非常有用的调试技术。不幸的是,我不记得我最初读到它的地方,但如果有人知道,请添加评论。

Here's an alternative answer than the window.onerror solution. This isn't something I've used in production, but is something that I just like because of the flexibility (i.e. you could use it to debug things like timing how long a method took or something).

Whilst you could probably pass window into it (don't quote me on that, and don't think it's a good idea) it does work if you have all your methods in an object:

(function(obj) {

    for (var name in obj) {
        if (typeof(obj[name]) == 'function') {
            currentMethod = obj[name];
            obj[name] = function() {
                try {
                    currentMethod();
                }
                catch (e) {
                    alert('Exception Handler: ' + e);
                }
            };
        }
    }

}(myObject));

Here's it working: http://jsfiddle.net/jonathon/kpYnW/

Basically, it goes through each property in your object, finds the ones that are functions and wraps them in a try/catch (or whatever else you want).

Whether or not it's efficient is a different matter - I've just found it a very useful technique for debugging. Unfortunately I can't remember the original place I read it but if anyone knows, please add as a comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文