diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index 79ccf235f..ab667c992 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -171,7 +171,7 @@ public class Checker extends AutomaticBean // TODO i18n throw new CheckstyleException( "cannot initialize module " - + name + " - " + ex.getMessage()); + + name + " - " + ex.getMessage(), ex); } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java index bc6eae7b2..cf6672464 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java @@ -160,17 +160,19 @@ class ConfigurationLoader return loader.getConfiguration(); } catch (FileNotFoundException e) { - throw new CheckstyleException("unable to find " + aConfigFname); + throw new CheckstyleException( + "unable to find " + aConfigFname, e); } catch (ParserConfigurationException e) { - throw new CheckstyleException("unable to parse " + aConfigFname); + throw new CheckstyleException( + "unable to parse " + aConfigFname, e); } catch (SAXException e) { throw new CheckstyleException("unable to parse " - + aConfigFname + " - " + e.getMessage()); + + aConfigFname + " - " + e.getMessage(), e); } catch (IOException e) { - throw new CheckstyleException("unable to read " + aConfigFname); + throw new CheckstyleException("unable to read " + aConfigFname, e); } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java index 35a7a2cc7..22d8cdaa9 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java @@ -163,7 +163,8 @@ public class PackageNamesLoader reader = new FileReader(aFilename); } catch (FileNotFoundException e) { - throw new CheckstyleException("unable to find " + aFilename); + throw new CheckstyleException( + "unable to find " + aFilename, e); } final InputSource source = new InputSource(reader); return loadModuleFactory(source, aFilename); @@ -186,17 +187,17 @@ public class PackageNamesLoader return nameLoader.getModuleFactory(); } catch (FileNotFoundException e) { - throw new CheckstyleException("unable to find " + aSourceName); + throw new CheckstyleException("unable to find " + aSourceName, e); } catch (ParserConfigurationException e) { - throw new CheckstyleException("unable to parse " + aSourceName); + throw new CheckstyleException("unable to parse " + aSourceName, e); } catch (SAXException e) { throw new CheckstyleException("unable to parse " - + aSourceName + " - " + e.getMessage()); + + aSourceName + " - " + e.getMessage(), e); } catch (IOException e) { - throw new CheckstyleException("unable to read " + aSourceName); + throw new CheckstyleException("unable to read " + aSourceName, e); } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java index 1e45a750b..272545e24 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java @@ -116,15 +116,15 @@ class PackageObjectFactory implements ModuleFactory } catch (ClassNotFoundException e) { throw new CheckstyleException( - "Unable to find class for " + aClassName); + "Unable to find class for " + aClassName, e); } catch (InstantiationException e) { throw new CheckstyleException( - "Unable to instantiate " + aClassName); + "Unable to instantiate " + aClassName, e); } catch (IllegalAccessException e) { throw new CheckstyleException( - "Unable to instantiate " + aClassName); + "Unable to instantiate " + aClassName, e); } } @@ -151,7 +151,7 @@ class PackageObjectFactory implements ModuleFactory } catch (CheckstyleException ex2) { throw new CheckstyleException( - "Unable to instantiate " + aName); + "Unable to instantiate " + aName, ex2); } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java index c8868fb96..9a4c93daa 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -295,7 +295,7 @@ public final class TreeWalker } catch (IllegalArgumentException ex) { throw new CheckstyleException("illegal token \"" - + token + "\" in check " + aCheck); + + token + "\" in check " + aCheck, ex); } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/AutomaticBean.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/AutomaticBean.java index 4ca638b7e..020f4e074 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/AutomaticBean.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/AutomaticBean.java @@ -170,27 +170,27 @@ public class AutomaticBean implements Configurable, Contextualizable throw new CheckstyleException( "Cannot set property '" + key + "' in module " + aConfiguration.getName() + " to '" + value - + "': " + e.getTargetException().getMessage()); + + "': " + e.getTargetException().getMessage(), e); } catch (IllegalAccessException e) { throw new CheckstyleException( "cannot access " + key + " in " - + this.getClass().getName()); + + this.getClass().getName(), e); } catch (NoSuchMethodException e) { throw new CheckstyleException( "cannot access " + key + " in " - + this.getClass().getName()); + + this.getClass().getName(), e); } catch (IllegalArgumentException e) { throw new CheckstyleException( "illegal value '" + value + "' for property '" + key - + "' of module " + aConfiguration.getName()); + + "' of module " + aConfiguration.getName(), e); } catch (ConversionException e) { throw new CheckstyleException( "illegal value '" + value + "' for property '" + key - + "' of module " + aConfiguration.getName()); + + "' of module " + aConfiguration.getName(), e); } } @@ -226,22 +226,22 @@ public class AutomaticBean implements Configurable, Contextualizable // + " is not interested in " + value) throw new CheckstyleException("cannot set property " + key + " to value " + value + " in bean " - + this.getClass().getName()); + + this.getClass().getName(), e); } catch (IllegalAccessException e) { throw new CheckstyleException( "cannot access " + key + " in " - + this.getClass().getName()); + + this.getClass().getName(), e); } catch (IllegalArgumentException e) { throw new CheckstyleException( "illegal value '" + value + "' for property '" + key - + "' of bean " + this.getClass().getName()); + + "' of bean " + this.getClass().getName(), e); } catch (ConversionException e) { throw new CheckstyleException( "illegal value '" + value + "' for property '" + key - + "' of bean " + this.getClass().getName()); + + "' of bean " + this.getClass().getName(), e); } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/CheckstyleException.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/CheckstyleException.java index d4dccb15b..956e0c2fc 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/CheckstyleException.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/CheckstyleException.java @@ -26,6 +26,9 @@ package com.puppycrawl.tools.checkstyle.api; */ public class CheckstyleException extends Exception { + /** the cause of this exception */ + private Throwable mCause = null; + /** * Creates a new CheckstyleException instance. * @@ -35,4 +38,48 @@ public class CheckstyleException extends Exception { super(aMessage); } + + /** + * Creates a new CheckstyleException instance + * that was caused by another exception. + * + * @param aMessage a message that explains this exception + * @param aCause the Exception that is wrapped by this exception + */ + public CheckstyleException(String aMessage, Throwable aCause) + { + super(aMessage); + initCause(aCause); + } + + /** + * Initializes the cause of this exception. + * In JDK 1.4 (and later) the cause is printed as part of + * the exception stacktrace. + * + * @param aCause the exception that caused this + * CheckstyleException to be thrown + * @return a reference to this CheckstyleException instance + */ + public synchronized Throwable initCause(Throwable aCause) + { + if (mCause != null) { + throw new IllegalStateException(); + } + if (mCause == this) { + throw new IllegalArgumentException(); + } + + mCause = aCause; + return this; + } + + /** + * @return the cause of this exception, might be null. + */ + public Throwable getCause() + { + return mCause; + } + }