VS2008 的 onerror 处理

发布于 2024-07-13 23:21:25 字数 604 浏览 6 评论 0原文

function testFun() {
    onerror = function() { log("caught the error"); return true; };
    setTimeout(function() { throw "bad bad bad"; }, 300);
};

这是示例代码,但它演示了一个问题。 如果我在 FF 或 IE7 中运行它,它会打印明智的“捕获错误”消息(假设有一个合理的“日志”函数)。

但是,如果我在 VS2008 中调试代码,调试器会在抛出时停止并显示以下消息:“Microsoft JScript 运行时错误:抛出异常且未捕获”。 如果我说“继续”或“忽略”,则不会生成日志消息。

这是一个问题,因为我正在使用的实际代码比这大得多,而且我偶尔会想要调试一些东西。 所以有两个问题:

  1. 有人知道为什么吗?我可以用一些我不知道的标志来修改这种行为吗?
  2. 我在这段代码中做了我认为我正在做的事情(设置全局“onerror”处理程序)吗? 如果不是,捕获此类错误的适当模式是什么?

注意:如果我使用 window.onerror 代替,这个问题没有区别。

function testFun() {
    onerror = function() { log("caught the error"); return true; };
    setTimeout(function() { throw "bad bad bad"; }, 300);
};

This is sample, code, but it demonstrates a problem.
If I run this in FF, or IE7, it prints the sensible "caught the error" message (assume a reasonable 'log' function).

However if I debug the code in VS2008, the debugger stops on the throw with the message: 'Microsoft JScript runtime error: Exception thrown and not caught'. If I say 'continue' or 'ignore', the log message is not produced.

This is a problem since the real code I am working with is much larger than this, and I'll occasionally want to, you know, debug stuff. So two questions:

  1. Any know why and can I modify this behaviour with some flag I don't know about?
  2. Am I doing what I think I'm doing (setting the global 'onerror' handler) in this code? If not, what is the appropriate pattern for catching this type of error?

Note: There is no difference wrt this problem if I use window.onerror instead.

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

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

发布评论

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

评论(1

[浮城] 2024-07-20 23:21:26

根据 this 定义全局 onerror 函数在 IE 中不起作用。 他们可能谈论的是 IE6 或更早版本,所以也许 MS 已经针对 IE7 修复了它 - 但我不希望它会自动流到 VS 调试器。

无论如何,请尝试使用 window.onerror = function 而不仅仅是 onerror

如果这不起作用,我猜你必须在计时器函数中使用 try/catch 块。

PS:获取firefox并使用firebug。 调试器(以及其他所有东西)比 VS 调试更好用

According to this defining a global onerror function doesn't work in IE. They were probably talking about IE6 or earlier, so maybe MS have fixed it for IE7 - however I wouldn't expect this to just automatically flow through to the VS debugger.

At any rate, try using window.onerror = function rather than just onerror.

If that doesn't work, you'll have to use a try/catch block inside your timer function I guess.

PS: Get firefox and use firebug. the debugger (and everything else) is much better and nicer to use than the VS debugging

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