View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.mail;
18  
19  import java.io.PrintStream;
20  import java.io.PrintWriter;
21  
22  /**
23   * Exception thrown when a checked error occurs in commons-email.
24   * <p>
25   * Supports nesting, emulating JDK 1.4 behavior if necessary.
26   * <p>
27   * Adapted from {@link org.apache.commons.collections.FunctorException}.
28   *
29   * @author jakarta-commons
30   * @since 1.0
31   * @version $Id: EmailException.java 279313 2005-09-07 12:41:58Z henning $
32   */
33  public class EmailException
34          extends Exception
35  {
36      /** Serializable version identifier */
37      static final long serialVersionUID = 5550674499282474616L;
38  
39      /**
40       * Does JDK support nested exceptions?
41       */
42      private static final boolean JDK_SUPPORTS_NESTED;
43  
44      static
45      {
46          boolean flag = false;
47  
48          try
49          {
50              Throwable.class.getDeclaredMethod("getCause", new Class[0]);
51              flag = true;
52          }
53          catch (NoSuchMethodException ex)
54          {
55              flag = false;
56          }
57  
58          JDK_SUPPORTS_NESTED = flag;
59      }
60  
61      /**
62       * Root cause of the exception
63       */
64      private final Throwable rootCause;
65  
66      /**
67       * Constructs a new <code>EmailException</code> with no
68       * detail message.
69       */
70      public EmailException()
71      {
72          super();
73          this.rootCause = null;
74      }
75  
76      /**
77       * Constructs a new <code>EmailException</code> with specified
78       * detail message.
79       *
80       * @param msg  the error message.
81       */
82      public EmailException(String msg)
83      {
84          super(msg);
85          this.rootCause = null;
86      }
87  
88      /**
89       * Constructs a new <code>EmailException</code> with specified
90       * nested <code>Throwable</code> root cause.
91       *
92       * @param rootCause  the exception or error that caused this exception
93       *                   to be thrown.
94       */
95      public EmailException(Throwable rootCause)
96      {
97          super(((rootCause == null) ? null : rootCause.getMessage()));
98          this.rootCause = rootCause;
99      }
100 
101     /**
102      * Constructs a new <code>EmailException</code> with specified
103      * detail message and nested <code>Throwable</code> root cause.
104      *
105      * @param msg  the error message.
106      * @param rootCause  the exception or error that caused this exception
107      *                   to be thrown.
108      */
109     public EmailException(String msg, Throwable rootCause)
110     {
111         super(msg);
112         this.rootCause = rootCause;
113     }
114 
115     /**
116      * Gets the cause of this throwable.
117      *
118      * @return  the cause of this throwable, or <code>null</code>
119      */
120     public Throwable getCause()
121     {
122         return rootCause;
123     }
124 
125     /**
126      * Prints the stack trace of this exception to the standard error stream.
127      */
128     public void printStackTrace()
129     {
130         printStackTrace(System.err);
131     }
132 
133     /**
134      * Prints the stack trace of this exception to the specified stream.
135      *
136      * @param out  the <code>PrintStream</code> to use for output
137      */
138     public void printStackTrace(PrintStream out)
139     {
140         synchronized (out)
141         {
142             PrintWriter pw = new PrintWriter(out, false);
143             printStackTrace(pw);
144 
145             // Flush the PrintWriter before it's GC'ed.
146             pw.flush();
147         }
148     }
149 
150     /**
151      * Prints the stack trace of this exception to the specified writer.
152      *
153      * @param out  the <code>PrintWriter</code> to use for output
154      */
155     public void printStackTrace(PrintWriter out)
156     {
157         synchronized (out)
158         {
159             super.printStackTrace(out);
160 
161             if ((rootCause != null) && (JDK_SUPPORTS_NESTED == false))
162             {
163                 out.print("Caused by: ");
164                 rootCause.printStackTrace(out);
165             }
166         }
167     }
168 }