no-useless-catch
禁止不必要的 catch
条款
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
只重新抛出原始错误的 catch
子句是多余的,对程序的运行时行为没有影响。这些多余的子句可能会造成混乱和代码膨胀,因此最好禁止这些不必要的 catch
子句。
¥A catch
clause that only rethrows the original error is redundant, and has no effect on the runtime behavior of the program. These redundant clauses can be a source of confusion and code bloat, so it’s better to disallow these unnecessary catch
clauses.
规则详情
¥Rule Details
此规则报告 catch
子句,只有 throw
捕获错误。
¥This rule reports catch
clauses that only throw
the caught error.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-useless-catch: "error"*/
try {
doSomethingThatMightThrow();
} catch (e) {
throw e;
}
try {
doSomethingThatMightThrow();
} catch (e) {
throw e;
} finally {
cleanUp();
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-useless-catch: "error"*/
try {
doSomethingThatMightThrow();
} catch (e) {
doSomethingBeforeRethrow();
throw e;
}
try {
doSomethingThatMightThrow();
} catch (e) {
handleError(e);
}
try {
doSomethingThatMightThrow();
} finally {
cleanUp();
}
何时不使用
¥When Not To Use It
如果你不想收到有关不必要的 catch 子句的通知,你可以安全地禁用此规则。
¥If you don’t want to be notified about unnecessary catch clauses, you can safely disable this rule.
版本
此规则是在 ESLint v5.11.0 中引入。