From 2d655f4684d55f747871cc1382d2444d73a85c2c Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 25 Apr 2013 18:35:06 +0200 Subject: [PATCH] pmd: fix #1087 PreserveStackTrace (still) ignores initCause() one more test, extended documentation example --- pmd/src/main/resources/rulesets/java/design.xml | 11 ++++++++--- .../lang/java/rule/design/xml/PreserveStackTrace.xml | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pmd/src/main/resources/rulesets/java/design.xml b/pmd/src/main/resources/rulesets/java/design.xml index 6609db8625..3731cadf0c 100644 --- a/pmd/src/main/resources/rulesets/java/design.xml +++ b/pmd/src/main/resources/rulesets/java/design.xml @@ -1353,14 +1353,19 @@ public class Foo { void good() { try{ Integer.parseInt("a"); - } catch(Exception e){ - throw new Exception(e); + } catch (Exception e) { + throw new Exception(e); // first possibility to create exception chain + } + try { + Integer.parseInt("a"); + } catch (Exception e) { + throw (IllegalStateException)new IllegalStateException().initCause(e); // second possibility to create exception chain. } } void bad() { try{ Integer.parseInt("a"); - } catch(Exception e){ + } catch (Exception e) { throw new Exception(e.getMessage()); } } diff --git a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/PreserveStackTrace.xml b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/PreserveStackTrace.xml index 9f326835f4..4b8fae2a99 100644 --- a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/PreserveStackTrace.xml +++ b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/PreserveStackTrace.xml @@ -462,6 +462,12 @@ public class Test { noSuchElementException.initCause(arrayIndexOutOfBoundsException); throw noSuchElementException; } + + try { + // do something + } catch (ArrayOutOfBoundsException e) { + throw (IllegalStateException)new IllegalStateException().initCause(e); + } } } ]]>