Ignore `catch` block in the `no-empty` rule
See original GitHub issueProblem
The following is very common pattern when you don’t care about an exception:
try {
foo();
} catch (err) {}
But it’s caught by the no-empty
rule.
I want the rule to catch empty blocks, but ignore the catch
block as it’s empty because it’s required by JS, not because I want it there.
I know I can put in a // empty
comment there, but that feels like an unuseful reiteration of the obvious.
Possible solution 1
Ignore catch
by default as it’s required to be empty and not really useful to require it not to be empty. This is the preferred solution.
Possible solution 2
Add an option that defines exceptions in the form of keywords:
{
"no-empty": [2, {"exceptions": "catch"}]
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (11 by maintainers)
Top Results From Across the Web
no-empty - ESLint - Pluggable JavaScript Linter
This rule disallows empty block statements. This rule ignores block statements which contain a comment (for example, in an empty catch or finally...
Read more >disallow empty block statements (no-empty) - ESLint
This rule ignores block statements which contain a comment (for example, in an empty catch or finally block of a try statement to...
Read more >Why are empty catch blocks a bad idea? - Stack Overflow
Usually empty try-catch is a bad idea because you are silently swallowing an error condition and then continuing execution.
Read more >empty_catches - Dart
In general, empty catch blocks should be avoided. In cases where they are intended, a comment should be provided to explain why exceptions...
Read more >Javadoc Comments - checkstyle
Checks the order of javadoc block-tags or javadoc tags. ... Ignore try block, but keep catch and finally blocks.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sindresorhus We provide a way to customize this behavior, if I’m not mistaken this code will lint just fine
Any comment inside a block will mark it as non-empty. Leaving comments in the cases where you used try catch but ignoring cache is a good idea, if somebody else is going to be looking at your code. But in general, I do see your point.
@sindresorhus
This is a HORRIBLE, HORRIBLE pattern that, unfortunately, is very common in javascript. But it shouldn’t be perpetuated explicitly or implicitly. At the very least you should provide a comment explaining why you are ignoring that exception. Better yet, you should do an
instanceof
check to only ignoreError
s of a certain type, and rethrow all others, which would also satisfy this rule.p.s. Excuse me for the strong formatting, but good error handling is something I feel VERY STRONGLY about. 😄