noUselessCatch
Summary
Section titled Summary- Rule available since:
v1.0.0
- Diagnostic Category:
lint/complexity/noUselessCatch
- This rule is recommended, which means is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-useless-catch
- Same as
Description
Section titled DescriptionDisallow 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.
Examples
Section titled ExamplesInvalid
Section titled Invalidtry { 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
Section titled Validtry { doSomething();} catch(e) { doSomethingWhenCatch(); throw e;}
try { doSomething();} catch(e) { handleError(e);}
try { doSomething();} finally { doCleanUp();}
How to configure
Section titled How to configure{ "linter": { "rules": { "complexity": { "noUselessCatch": "error" } } }}