Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: tried to make the rule description more precise

Programmers often suppress checked exceptions . That is, they often catch by catching exceptions with a an empty or trivial catch block, with the catch body doing nothing or something trivial, such as printing the stack trace. Often the catch body will have a comment stating that the exception is explicitly being ignored.

Exceptions must be handled appropriately. There are few valid reasons for suppressing exceptions; the most common are cases where the client cannot be expected to recover from the underlying problem. In these cases, it is good practice to allow the exception to propagate outwards rather than to catch and suppress the exception.

. Each catch block must ensure that the program continues only with valid invariants. Consequently, the catch block must either recover from the exceptional condition, re-throw the exception to allow the next nearest dynamically-enclosing catch clause of a try statement to recover, or throw an exception that is appropriate to the context of the catch block.

Catching and suppressing exceptions is considered bad practice for several reasons. Exceptions disrupt the expected control flow of the application. For example, statements in the try block that follow the statement that caused the exception are skipped. Each catch block must ensure that the program continues only with valid invariants. Consequently, the catch block must either recover from the exceptional condition, re-throw the exception to allow a higher level of abstraction to attempt recovery, or throw an exception that is appropriate to the context of the catch block. When recovery is possible, any instructions inside the the try block whose execution is required must be moved outside the try block to ensure that they are executedno part of any expressions or statement that occurs after the point from which the exception is thrown may appear to have been evaluated. Consequently, exceptions must be handled appropriately. There are few valid reasons for suppressing exceptions; the most common are cases where the client cannot be expected to recover from the underlying problem. In these cases, it is good practice to allow the exception to propagate outwards rather than to catch and suppress the exception.

Noncompliant Code Example

...