Exception handling - rules provided by PMD
Strict Exceptions
AvoidCatchingThrowable
Catching Throwable errors is not recommended since its scope is very broad.
It includes runtime issues such as OutOfMemoryError that should be exposed and managed separately.
SignatureDeclareThrowsException
Methods that declare the generic Exception as a possible throwable are not very helpful since their failure modes are unclear.
Use a class derived from RuntimeException or a more specific checked exception.
ExceptionAsFlowControl
Using Exceptions as form of flow control is not recommended as they obscure true exceptions when debugging.
Either add the necessary validation or use an alternate control structure.
AvoidCatchingNPE
Code should never throw NullPointerExceptions under normal circumstances.
A catch block may hide the original error, causing other, more subtle problems later on.
AvoidThrowingRawExceptionTypes
Avoid throwing certain exception types.
Rather than throw a raw RuntimeException, Throwable, Exception or Error, use a subclassed exception or error instead.
AvoidThrowingNullPointerException
Avoid throwing NullPointerExceptions.
These are confusing because most people will assume that the virtual machine threw it.
Consider using an IllegalArgumentException instead; this will beclearly seen as a programmer-initiated exception.
AvoidRethrowingException
Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity.
DoNotExtendJavaLangError
Errors are system exceptions. Do not extend them.
DoNotThrowExceptionInFinally
Throwing exceptions within a 'finally' block is confusing since they may mask other exceptions or code defects.
Note: This is a PMD implementation of the Lint4j rule "A throw in a finally block"
AvoidThrowingNewInstanceOfSameException
Catch blocks that merely rethrow a caught exception wrapped inside a new instance of the same type only add tocode size and runtime complexity.
AvoidCatchingGenericException
Avoid catching generic exceptions such as NullPointerException, RuntimeException, Exception in try-catch block
AvoidLosingExceptionInformation
Statements in a catch block that invoke accessors on the exception without using the informationonly add to code size.
Either remove the invocation, or use the return result.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.