Inspectopedia
 
2024.3

Catch block may ignore exception

Warning
New
Last modified: 03 December 2024

Reports catch blocks that are empty or may ignore an exception.

While occasionally intended, empty catch blocks may complicate debugging. Also, ignoring a catch parameter might be wrong. Finally, the static code analyzer reports if it detects that a catch block may silently ignore important VM exceptions like NullPointerException. Ignoring such an exception (without logging or rethrowing it) may hide a bug.

The inspection won't report any catch parameters named ignore or ignored. Conversely, the inspection will warn you about any catch parameters named ignore or ignored that are actually in use. Additionally, the inspection won't report catch parameters inside test sources named expected or ok.

You can use a quick-fix to change the exception name to ignored. For empty catch blocks, an additional quick-fix to generate the catch body is suggested. You can modify the "Catch Statement Body" template on the Code tab in Settings | Editor | File and Code Templates.

Example:

After the quick-fix is applied:

Configure the inspection:

  • Use the Do not warn when 'catch' block contains a comment option to ignore catch blocks with comments.

  • Use the Do not warn when 'catch' block is not empty option to ignore catch blocks that contain statements or comments inside, while the variable itself is not used.

  • Use the Do not warn when exception named 'ignore(d)' is not actually ignored option to ignore variables named ignored if they are in use.

New in 2018.1