Skip to content

no-empty-catch

Reports a catch, except or rescue clause whose body has no statement and no comment (catch (e) { }; in Python a lone pass counts as no statement). A comment belongs to the clause when it is on a later line than the clause starts on, after the clause’s last token on that line, or indented deeper than the clause. With the option comment_handles off a comment does not count either. With broad_only only a clause that catches everything, or one of the exception types of the option types, is reported.

An empty handler swallows the error without a trace: the program goes on in a state it did not expect, and the failure shows up later far from its cause, with nothing in the logs.

To fix, handle the error, log it, or let it propagate; if ignoring it is right, say why in a comment.

Reported
...
void f(int x) {
try {
g();
} catch (Exception e) {
}
try {
g();
...
c:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
cpp:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
csharp:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
java:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
js:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
kotlin:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
php:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
powershell:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
python:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
ruby:
lint:
rules:
no-empty-catch:
level: warn # off, warn (default), error
options:
comment_handles: true
broad_only: false
types: []
Option Type Default Description
comment_handles bool true A comment in the body counts as handling the error
broad_only bool false Report only a clause that catches everything or a broad type
types list [] Types that count as broad

Enabled by the presets: js/eslint-recommended, powershell/psscriptanalyzer, python/ruff, ruby/rubocop, csharp/roslynator.

Suppress one line with sudsy-disable-next-line no-empty-catch in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
checkstyle EmptyCatchBlock full
eslint no-empty partial the allowEmptyCatch option is not supported
psscriptanalyzer PSAvoidUsingEmptyCatchBlock full
Roslynator RCS1075 full
rubocop Lint/SuppressedException full
ruff S110 partial The preset sets option comment_handles off because ruff reports an except whose only statement is pass, a comment or not.