001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by                                                          *
009     *****************************************************************************/
010    package org.picocontainer;
011    
012    import java.io.PrintStream;
013    import java.io.PrintWriter;
014    
015    /**
016     * Superclass for all Exceptions in PicoContainer. You can use this if you want to catch all exceptions thrown by
017     * PicoContainer. Be aware that some parts of the PicoContainer API will also throw {@link NullPointerException} when
018     * <code>null</code> values are provided for method arguments, and this is not allowed.
019     * 
020     * @author Paul Hammant
021     * @author Aslak Hellesøy
022     * @version $Revision: 1812 $
023     * @since 1.0
024     */
025    public abstract class PicoException extends RuntimeException {
026        /**
027         * The exception that caused this one.
028         */
029        private Throwable cause;
030    
031        /**
032         * Construct a new exception with no cause and no detail message. Note modern JVMs may still track the exception
033         * that caused this one.
034         */
035        protected PicoException() {
036        }
037    
038        /**
039         * Construct a new exception with no cause and the specified detail message.  Note modern JVMs may still track the
040         * exception that caused this one.
041         *
042         * @param message the message detailing the exception.
043         */
044        protected PicoException(final String message) {
045            super(message);
046        }
047    
048        /**
049         * Construct a new exception with the specified cause and no detail message.
050         * 
051         * @param cause the exception that caused this one.
052         */
053        protected PicoException(final Throwable cause) {
054            this.cause = cause;
055        }
056    
057        /**
058         * Construct a new exception with the specified cause and the specified detail message.
059         *
060         * @param message the message detailing the exception.
061         * @param cause   the exception that caused this one.
062         */
063        protected PicoException(final String message, final Throwable cause) {
064            super(message);
065            this.cause = cause;
066        }
067    
068        /**
069         * Retrieve the exception that caused this one.
070         *
071         * @return the exception that caused this one, or null if it was not set.
072         * @see Throwable#getCause() the method available since JDK 1.4 that is overridden by this method.
073         */
074        public Throwable getCause() {
075            return cause;
076        }
077    
078        /**
079         * Overridden to provide 1.4 style stack traces on pre-1.4.
080         * 
081         * @param s the {@link PrintStream} used to print the stack trace
082         */
083        public void printStackTrace() {
084            printStackTrace(System.err);
085        }
086    
087        /**
088         * Overridden to provide 1.4 style stack traces on pre-1.4.
089         */
090        public void printStackTrace(PrintStream s) {
091            super.printStackTrace(s);
092            if(cause!=null) {
093                s.println("Caused by:\n");
094                cause.printStackTrace(s);
095            }
096        }
097    
098        /**
099         * Overridden to provide 1.4 style stack traces on pre-1.4.
100         * 
101         * @param s the {@link PrintWriter} used to print the stack trace
102         */
103        public void printStackTrace(PrintWriter s) {
104            super.printStackTrace(s);
105            if(cause!=null) {
106                s.println("Caused by:\n");
107                cause.printStackTrace(s);
108            }
109        }
110    }