Java language related problems

A return in a finally block

A return statement in a finally block makes the control flow hard to understand; if the statement returns with a value, the try statement will always return with this value, ignoring a previous return value.

Solution: remove the return statement.

Severity level: 1

A throw in a finally block

A throw statement in a finally block makes the control flow hard to understand. It will mask an exception or a successful return from the try block, and make appear business code fail when simply the cleanup code failed.

Solution: refactor the exception handling in the try block

Severity level: 1

Empty catch blocks

Empty catch blocks supposed to handle checked exceptions that are not expected to ever happen should handle this case in a more robust way.

Solution: add a Runtime exception to the catch block (e.g. throw new IllegalStateException();, or use assert false;.

Severity level: 2

Catching Error or Throwable

Errors shouldn't be caught by user code without rethrowing, as recommended by Sun; they indicate a severe problem that can't be handled by user code, e.g. out of memory.

Solution: Revisit exception handling, and

Severity level: 2

Switch statements without default branch

Switch statements without a default branch will become a liability once the value range is expanded.

Solution: add a default branch that throws a runtime exception, such as java.lang.IllegalStateException, or use assert false;

Severity level: 3

Case statements that may fall through

Case statements fall through if not terminated by a return, break or a throw statement, as explained in section 4.11 of the JLS. This often occurs because of a forgotten break statement.

Solution: add a control statement, if necessary

Severity level: 3

Pathological Switch statements

A switch statement with less than three branches is better expressed as an if statement, and may be a result of incomplete coding, or a branch that has been commented out.

Solution: replace by an if statement.

Severity level: 3