没有 try 块的代码将抛出异常。但有了block,就不会了

发布于 2024-12-25 04:56:19 字数 1231 浏览 0 评论 0原文

我编写了一个 用户脚本,并在 Greasemonkey 0.9.13 中运行它。

如果我删除唯一的 try...catch 块(第 54-66 行),但将代码保留在其中,它将抛出如下异常:

未捕获的异常:[异常...“不支持操作”代码: “9”nsresult:“0x80530009(NS_ERROR_DOM_NOT_SUPPORTED_ERR)”位置: “resource://greasemonkey/runScript.js 行:29”]

然而,剥离的脚本在 Chrome 中运行良好,没有任何问题。

但是,它不会将任何内容打印到控制台中,这意味着如果用 try 块包装,try 块中的代码不会抛出异常。

你们能告诉我为什么它有效吗?

这是链接源中的代码片段:

var streamItems = $('div.main-content div.stream-item');
var streamItemsLength = streamItems.length;
var innerHeight = window.innerHeight;
var scrollY = window.scrollY;
var y = scrollY + innerHeight;
var tweet;
var tweetHeight = 0;
try {
  for (var sumHeight = getHeaderHeight(), num = 0; sumHeight < y; num++, sumHeight += tweetHeight) {
    tweet = streamItems[num];
    tweetHeight = getHeight(tweet);
    if (tweetHeight == 0) {
      removeClass(tweet, 'hidden-tweet');
      tweetHeight = getHeight(tweet);
    }
  }
}
catch (e) {
  console.log(e.stack);
}

I have written a userscript, and run it in Greasemonkey 0.9.13.

If I remove the only try... catch block (line 54-66) but keep the code inside it, it will throw exceptions like below:

uncaught exception: [Exception... "Operation is not supported" code:
"9" nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)" location:
"resource://greasemonkey/runScript.js Line: 29"]

However, stripped scripts run well in Chrome w/o any problems.

However, it will NOT print anything into console, which means codes in try block don't throw exception if wrapped by try block.

Can you guys tell me why it works?

This is the code snippet from that linked source:

var streamItems = $('div.main-content div.stream-item');
var streamItemsLength = streamItems.length;
var innerHeight = window.innerHeight;
var scrollY = window.scrollY;
var y = scrollY + innerHeight;
var tweet;
var tweetHeight = 0;
try {
  for (var sumHeight = getHeaderHeight(), num = 0; sumHeight < y; num++, sumHeight += tweetHeight) {
    tweet = streamItems[num];
    tweetHeight = getHeight(tweet);
    if (tweetHeight == 0) {
      removeClass(tweet, 'hidden-tweet');
      tweetHeight = getHeight(tweet);
    }
  }
}
catch (e) {
  console.log(e.stack);
}

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

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

发布评论

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

评论(2

随风而去 2025-01-01 04:56:19

该脚本需要 try 块(如您所见),因为 for 循环的逻辑非常差(它经常会溢出 streamItems数组,并且在删除 hidden-tweet 类时可能存在竞争条件)。

你说它在 Chrome 中不会抛出异常,但看起来这只是偶然。如果条件合适,它在 Chrome 中也应该会失败——或者也许 Chrome 的节点操作完全不同。没关系,代码很差。

至于它不向控制台打印任何内容,你确定吗?在我的测试中确实如此。

但有时在匿名函数包装器和事件侦听器之间,错误 stack 可能会显示为空。

That script needs the try block (as you can see) because the logic of the for loop is very poor (it will often overrun the streamItems array and probably has a race condition when removing the hidden-tweet class).

You say it doesn't throw exceptions in Chrome, but it looks like this is just happenstance. With the right conditions, it should fail in Chrome too -- or maybe Chrome's node manipulation is just enough different. It doesn't matter, the code is poor.

As for it not printing anything to the console, are you sure? It does in my tests.

But sometimes between anonymous function wrappers and event listeners, maybe the error stack might appear to be empty.

祁梦 2025-01-01 04:56:19

我们使用 try/catch 来捕获 try 块中代码中发生的任何错误。根据您的要求,您可以决定是否要通过在 catch 块中添加一些代码来抑制消息(或)警告用户。您可以添加一些警报并查看消息以进行调试。

根据异常消息,您似乎正在对不支持的 DOM 对象执行操作(方法/函数调用),您需要重新访问代码并查看您正在执行的函数调用是否受支持。

We use try/catch to catch any errors happened in code which is in try block. Based on your requirement you can decide whether you want to suppress the message (or) alert the user by adding some code in catch block. You can add some alert and see the message for your debugging purpose.

Based on exception message, it seems you are performing an operation (method/function call) on DOM object which is not supported, you need to revisit the code and see the function calls you are doing are supported.

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