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:
@ -1,5 +1,7 @@
|
|||||||
???? - 4.2.6:
|
???? - 4.2.6:
|
||||||
|
|
||||||
|
New rule:
|
||||||
|
StrictExceptions : AvoidCatchingGenericException
|
||||||
|
|
||||||
February 08, 2009 - 4.2.5:
|
February 08, 2009 - 4.2.5:
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ public class StrictExceptionRulesTest extends SimpleAggregatorTst {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
addRule("strictexception", "AvoidCatchingGenericException");
|
||||||
addRule("strictexception", "AvoidCatchingNPE");
|
addRule("strictexception", "AvoidCatchingNPE");
|
||||||
addRule("strictexception", "AvoidCatchingThrowable");
|
addRule("strictexception", "AvoidCatchingThrowable");
|
||||||
addRule("strictexception", "AvoidRethrowingException");
|
addRule("strictexception", "AvoidRethrowingException");
|
||||||
|
@ -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>
|
@ -330,5 +330,48 @@ public class Foo {
|
|||||||
</example>
|
</example>
|
||||||
</rule>
|
</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>
|
</ruleset>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user