Home > Code Analysis > Code Issues – Catch block is empty

Code Issues – Catch block is empty

February 10th, 2011

Cause:

This code issue shows a warning when the catch block is empty. Usually empty catch block is a bad idea, because you are silently swallowing an error condition and then continuing an execution of the program, or the program may simply fail for an unknown reason.

In general, when an exception occurs, it can be thrown up to the caller, or it can be caught in a catch block. When catching an exception, it is recommended to inform the user about the problem, or log it for a future review.

Deciding what exactly to do depends on the nature of the problem. If there is an actual bug in the program, e.g. a defect that needs to be fixed, then one might do all of the possible solutions to let the user know about the problem and ask him to send some technical details to a program developer.

If the exception does not represent a bug, then different behavior may be appropriate. For example, if a problem with user input is detected and an exception is thrown as a result, then merely informing the user of the problem might be all that is required.

Sample:

CodeRush Code Issues - Catch block is empty

How to fix:

  • Add the diagnostic message logging capability inside the catch block, e.g.,:

CodeRush Catch Block Is Empty Fix

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/12/2012
ID: C067

Similar Posts: