Skip to content
← Back to rules

eslint/no-useless-catch 正确性

This rule is turned on by default.

它做了什么

禁止不必要的 catch 子句

为什么这是个问题?

仅重新抛出原始错误的 catch 子句是冗余的,对程序的运行时行为没有任何影响。这些冗余的子句可能会引起混淆并导致代码膨胀,因此最好禁止这些不必要的 catch 子句。

示例

此规则的 错误 代码示例:

javascript
try {
  doSomethingThatMightThrow();
} catch (e) {
  throw e;
}

此规则的 正确 代码示例:

javascript
doSomethingThatMightThrow();

如何使用

要通过配置文件或在 CLI 中 启用 此规则,可以使用:

json
{
  "rules": {
    "no-useless-catch": "error"
  }
}
bash
oxlint --deny no-useless-catch

参考资料