noUselessCatch (since v12.0.0)
This rule is recommended by Rome.
Disallow unnecessary catch
clauses.
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.
Source: https://eslint.org/docs/latest/rules/no-useless-catch
Examples
Invalid
try {
doSomething();
} catch(e) {
throw e;
}
complexity/noUselessCatch.js:4:5 lint/complexity/noUselessCatch ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The catch clause that only rethrows the original error is redundant.
2 │ doSomething();
3 │ } catch(e) {
> 4 │ throw e;
│ ^^^^^^^^
5 │ }
6 │
ℹ These unnecessary catch clauses can be confusing. It is recommended to remove them.
try {
doSomething();
} catch(e) {
throw e;
} finally {
doCleanUp();
}
complexity/noUselessCatch.js:4:5 lint/complexity/noUselessCatch ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The catch clause that only rethrows the original error is redundant.
2 │ doSomething();
3 │ } catch(e) {
> 4 │ throw e;
│ ^^^^^^^^
5 │ } finally {
6 │ doCleanUp();
ℹ These unnecessary catch clauses can be confusing. It is recommended to remove them.
Valid
try {
doSomething();
} catch(e) {
doSomethingWhenCatch();
throw e;
}
try {
doSomething();
} catch(e) {
handleError(e);
}