[java] Add DoNotExtendJavaLangThrowable rule

Copy and pasted code from DoNotExtendJavaLangError rule.

Resolves #367
https://github.com/pmd/pmd/issues/367
This commit is contained in:
Robert Painsi
2017-10-14 15:59:14 +02:00
parent 598c7910a3
commit 82b219428b
5 changed files with 88 additions and 1 deletions

View File

@ -326,6 +326,7 @@ List of rulesets and rules contained in each ruleset.
* [AvoidThrowingNullPointerException](pmd_rules_java_strictexception.html#avoidthrowingnullpointerexception): Avoid throwing NullPointerExceptions. These are confusing because most people will assume that th...
* [AvoidThrowingRawExceptionTypes](pmd_rules_java_strictexception.html#avoidthrowingrawexceptiontypes): Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable,Excep...
* [DoNotExtendJavaLangError](pmd_rules_java_strictexception.html#donotextendjavalangerror): Errors are system exceptions. Do not extend them.
* [DoNotExtendJavaLangThrowable](pmd_rules_java_strictexception.html#donotextendjavalangthrowable): Extend Exception or RuntimeException instead of Throwable.
* [DoNotThrowExceptionInFinally](pmd_rules_java_strictexception.html#donotthrowexceptioninfinally): Throwing exceptions within a 'finally' block is confusing since they may mask other exceptions or...
* [ExceptionAsFlowControl](pmd_rules_java_strictexception.html#exceptionasflowcontrol): Using Exceptions as form of flow control is not recommended as they obscure true exceptions when ...
* [SignatureDeclareThrowsException](pmd_rules_java_strictexception.html#signaturedeclarethrowsexception): Methods that declare the generic Exception as a possible throwable are not very helpful since the...

View File

@ -5,7 +5,7 @@ permalink: pmd_rules_java_strictexception.html
folder: pmd/rules/java
sidebaractiveurl: /pmd_rules_java.html
editmepath: ../pmd-java/src/main/resources/rulesets/java/strictexception.xml
keywords: Strict Exceptions, AvoidCatchingThrowable, SignatureDeclareThrowsException, ExceptionAsFlowControl, AvoidCatchingNPE, AvoidThrowingRawExceptionTypes, AvoidThrowingNullPointerException, AvoidRethrowingException, DoNotExtendJavaLangError, DoNotThrowExceptionInFinally, AvoidThrowingNewInstanceOfSameException, AvoidCatchingGenericException, AvoidLosingExceptionInformation
keywords: Strict Exceptions, AvoidCatchingThrowable, SignatureDeclareThrowsException, ExceptionAsFlowControl, AvoidCatchingNPE, AvoidThrowingRawExceptionTypes, AvoidThrowingNullPointerException, AvoidRethrowingException, DoNotExtendJavaLangError, DoNotExtendJavaLangThrowable, DoNotThrowExceptionInFinally, AvoidThrowingNewInstanceOfSameException, AvoidCatchingGenericException, AvoidLosingExceptionInformation
---
## AvoidCatchingGenericException
@ -310,6 +310,30 @@ public class Foo extends Error { }
<rule ref="rulesets/java/strictexception.xml/DoNotExtendJavaLangError" />
```
## DoNotExtendJavaLangThrowable
**Since:** PMD 6.0
**Priority:** Medium (3)
Extend Exception or RuntimeException instead of Throwable.
```
//ClassOrInterfaceDeclaration/ExtendsList/ClassOrInterfaceType
[@Image="Throwable" or @Image="java.lang.Throwable"]
```
**Example(s):**
``` java
public class Foo extends Throwable { }
```
**Use this rule by referencing it:**
``` xml
<rule ref="rulesets/java/strictexception.xml/DoNotExtendJavaLangThrowable" />
```
## DoNotThrowExceptionInFinally
**Since:** PMD 4.2