KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > MessagingException


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)MessagingException.java 1.15 06/03/09
24  *
25  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail;
29
30 import java.lang.*;
31
32 /**
33  * The base class for all exceptions thrown by the Messaging classes
34  *
35  * @author John Mani
36  * @author Bill Shannon
37  */

38
39 public class MessagingException extends Exception JavaDoc {
40
41     /**
42      * The next exception in the chain.
43      *
44      * @serial
45      */

46     private Exception JavaDoc next;
47
48     private static final long serialVersionUID = -7569192289819959253L;
49
50     /**
51      * Constructs a MessagingException with no detail message.
52      */

53     public MessagingException() {
54     super();
55     initCause(null); // prevent anyone else from setting it
56
}
57
58     /**
59      * Constructs a MessagingException with the specified detail message.
60      *
61      * @param s the detail message
62      */

63     public MessagingException(String JavaDoc s) {
64     super(s);
65     initCause(null); // prevent anyone else from setting it
66
}
67
68     /**
69      * Constructs a MessagingException with the specified
70      * Exception and detail message. The specified exception is chained
71      * to this exception.
72      *
73      * @param s the detail message
74      * @param e the embedded exception
75      * @see #getNextException
76      * @see #setNextException
77      * @see #getCause
78      */

79     public MessagingException(String JavaDoc s, Exception JavaDoc e) {
80     super(s);
81     next = e;
82     initCause(null); // prevent anyone else from setting it
83
}
84
85     /**
86      * Get the next exception chained to this one. If the
87      * next exception is a MessagingException, the chain
88      * may extend further.
89      *
90      * @return next Exception, null if none.
91      */

92     public synchronized Exception JavaDoc getNextException() {
93     return next;
94     }
95
96     /**
97      * Overrides the <code>getCause</code> method of <code>Throwable</code>
98      * to return the next exception in the chain of nested exceptions.
99      *
100      * @return next Exception, null if none.
101      */

102     public synchronized Throwable JavaDoc getCause() {
103     return next;
104     }
105
106     /**
107      * Add an exception to the end of the chain. If the end
108      * is <strong>not</strong> a MessagingException, this
109      * exception cannot be added to the end.
110      *
111      * @param ex the new end of the Exception chain
112      * @return <code>true</code> if this Exception
113      * was added, <code>false</code> otherwise.
114      */

115     public synchronized boolean setNextException(Exception JavaDoc ex) {
116     Exception JavaDoc theEnd = this;
117     while (theEnd instanceof MessagingException JavaDoc &&
118            ((MessagingException JavaDoc)theEnd).next != null) {
119         theEnd = ((MessagingException JavaDoc)theEnd).next;
120     }
121     // If the end is a MessagingException, we can add this
122
// exception to the chain.
123
if (theEnd instanceof MessagingException JavaDoc) {
124         ((MessagingException JavaDoc)theEnd).next = ex;
125         return true;
126     } else
127         return false;
128     }
129
130     /**
131      * Override toString method to provide information on
132      * nested exceptions.
133      */

134     public synchronized String JavaDoc toString() {
135     String JavaDoc s = super.toString();
136     Exception JavaDoc n = next;
137     if (n == null)
138         return s;
139     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s == null ? "" : s);
140     while (n != null) {
141         sb.append(";\n nested exception is:\n\t");
142         sb.append(n.toString());
143         if (n instanceof MessagingException JavaDoc) {
144         MessagingException JavaDoc mex = (MessagingException JavaDoc)n;
145         n = mex.next;
146         } else {
147         n = null;
148         }
149     }
150     return sb.toString();
151     }
152 }
153
Popular Tags