Skip to content
← Back to rules

eslint/no-useless-catch Correctness

This rule is turned on by default.

它的作用

禁止不必要的 catch 子句。

为什么这不好?

只会重新抛出原始错误的 catch 子句是多余的, 并且对程序的运行时行为没有任何影响。 这些多余的子句可能会造成混淆并增加代码冗余, 因此最好禁止这些不必要的 catch 子句。

示例

此规则的错误代码示例:

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

此规则的正确代码示例:

javascript
doSomethingThatMightThrow();

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-useless-catch": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-useless-catch": "error",
  },
});
bash
oxlint --deny no-useless-catch

版本

此规则已在 v0.0.5 中添加。

参考资料