Versions

no-useless-catch

Disallow unnecessary catch clauses

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

一个只重新抛出原始错误的 catch 子句是多余的,对程序的运行行为没有影响。这些多余的子句可能是混乱和代码膨胀的来源,所以最好不允许这些不必要的 catch 子句。

规则细节

这条规则报告的 catch 子句只 throw 被捕获的错误。

使用此规则的错误示例:

Open in Playground
/*eslint no-useless-catch: "error"*/

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

try {
  doSomethingThatMightThrow();
} catch (e) {
  throw e;
} finally {
  cleanUp();
}

使用此规则的正确示例:

Open in Playground
/*eslint no-useless-catch: "error"*/

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

try {
  doSomethingThatMightThrow();
} catch (e) {
  handleError(e);
}

try {
  doSomethingThatMightThrow();
} finally {
  cleanUp();
}

何时不用

如果你不希望被通知到不必要的捕获条款,你可以安全地禁用这个规则。

Version

This rule was introduced in ESLint v5.11.0.

Resources

更改语言