New rule in strictexceptions : AvoidCatchingGenericException

patch submitted by Nadhamuni Kothapalle

https://sourceforge.net/tracker/?func=detail&atid=479923&aid=2591604&group_id=56262


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6842 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch 2009-02-12 19:28:38 +00:00
parent a31f7f3578
commit a846ccb014
4 changed files with 95 additions and 1 deletions

View File

@ -1,5 +1,7 @@
???? - 4.2.6:
New rule:
StrictExceptions : AvoidCatchingGenericException
February 08, 2009 - 4.2.5:

View File

@ -8,6 +8,7 @@ public class StrictExceptionRulesTest extends SimpleAggregatorTst {
@Before
public void setUp() {
addRule("strictexception", "AvoidCatchingGenericException");
addRule("strictexception", "AvoidCatchingNPE");
addRule("strictexception", "AvoidCatchingThrowable");
addRule("strictexception", "AvoidRethrowingException");

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
failure case
]]></description>
<expected-problems>3</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {
} catch (NullPointerException e) {
} catch (Exception e) {
} catch (RuntimeException e) {
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
catching another type, ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {
} catch (FooException e) {
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
throwing it, ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
throw new NullPointerException();
}
}
]]></code>
</test-code>
</test-data>

View File

@ -330,5 +330,48 @@ public class Foo {
</example>
</rule>
<rule name="AvoidCatchingGenericException"
since="4.2.5"
message="Avoid catching generic exceptions such as NullPointerException, RuntimeException, Exception in try-catch block"
externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#AvoidCatchingGenericException"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
Avoid catching generic exceptions such as NullPointerException, RuntimeException, Exception in try-catch block
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//CatchStatement/FormalParameter/Type/ReferenceType/ClassOrInterfaceType[
@Image='NullPointerException' or
@Image='Exception' or
@Image='RuntimeException']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
package com.igate.primitive;
public class PrimitiveType {
public void downCastPrimitiveType() {
try {
System.out.println(" i [" + i + "]");
} catch(Exception e) {
e.printStackTrace();
} catch(RuntimeException e) {
e.printStackTrace();
} catch(NullPointerException e) {
e.printStackTrace();
}
}
}
]]>
</example>
</rule>
</ruleset>