noUselessCatch
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/complexity/noUselessCatch¥Diagnostic Category:
lint/complexity/noUselessCatch -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-useless-catch相同¥Same as
no-useless-catch
-
¥How to configure
{ "linter": { "rules": { "complexity": { "noUselessCatch": "error" } } }}¥Description
禁止不必要的 catch 子句。
¥Disallow unnecessary catch clauses.
仅重新抛出原始错误的 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.
¥Examples
¥Invalid
try { doSomething();} catch(e) { throw e;}code-block.js:4:5 lint/complexity/noUselessCatch ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The catch clause that only rethrows the original error is useless.
2 │ doSomething();
3 │ } catch(e) {
> 4 │ throw e;
│ ^^^^^^^^
5 │ }
6 │
ℹ An unnecessary catch clause can be confusing.
try { doSomething();} catch(e) { throw e;} finally { doCleanUp();}code-block.js:4:5 lint/complexity/noUselessCatch FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The catch clause that only rethrows the original error is useless.
2 │ doSomething();
3 │ } catch(e) {
> 4 │ throw e;
│ ^^^^^^^^
5 │ } finally {
6 │ doCleanUp();
ℹ An unnecessary catch clause can be confusing.
ℹ Unsafe fix: Remove the catch clause.
1 1 │ try {
2 2 │ doSomething();
3 │ - }·catch(e)·{
4 │ - ····throw·e;
5 │ - }·finally·{
3 │ + }·finally·{
6 4 │ doCleanUp();
7 5 │ }
¥Valid
try { doSomething();} catch(e) { doSomethingWhenCatch(); throw e;}try { doSomething();} catch(e) { handleError(e);}try { doSomething();} finally { doCleanUp();}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号