KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > exceptions > NestedException


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.util.exceptions;
47
48 import java.io.PrintStream JavaDoc;
49 import java.io.PrintWriter JavaDoc;
50
51 /**
52  * Created by IntelliJ IDEA.
53  * User: moti-pc
54  * Date: Feb 9, 2005
55  * Time: 6:00:08 PM
56  * Holds nested exceptions an print it in orderliness.
57  */

58 public class NestedException extends Exception JavaDoc
59 {
60
61     private Throwable JavaDoc m_nestedException;
62
63     public NestedException() {
64       super();
65     }
66
67     public NestedException(String JavaDoc message) {
68       super(message);
69     }
70
71     public NestedException(String JavaDoc message, Throwable JavaDoc nestedException) {
72         super(message);
73         m_nestedException = nestedException;
74     }
75
76     public Throwable JavaDoc getOriginatingException() {
77         return m_nestedException;
78     }
79
80     /**
81      * Gets the message attribute of the InitializationFatalException object
82      *
83      * @return The message value
84      */

85     public String JavaDoc getMessage() {
86         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
87         String JavaDoc msg = super.getMessage();
88         if (msg == null) {
89             msg = "";
90         }
91         buff.append(msg);
92         if (m_nestedException != null) {
93             buff.append(" Cause: ");
94             buff.append(m_nestedException.getMessage());
95         }
96         return buff.toString();
97     }
98
99     /**
100      * Prints this <code>Throwable</code> and its backtrace to the
101      * specified print stream.
102      *
103      * @param s <code>PrintStream</code> to use for output
104      */

105     public void printStackTrace(PrintStream JavaDoc s) {
106         super.printStackTrace(s);
107         if (m_nestedException != null) {
108             s.println("----- Cause Exception -----");
109             m_nestedException.printStackTrace(s);
110         }
111     }
112
113     /**
114      * Prints this <code>Throwable</code> and its backtrace to the specified
115      * print writer.
116      *
117      * @param s <code>PrintWriter</code> to use for output
118      * @since JDK1.1
119      */

120     public void printStackTrace(PrintWriter JavaDoc s) {
121         super.printStackTrace(s);
122         if (m_nestedException != null) {
123             s.println("----- Cause Exception -----");
124             m_nestedException.printStackTrace(s);
125         }
126     }
127
128     /**
129      * Prints this <code>Throwable</code> and its backtrace to the
130      * standard error stream. This method prints a stack trace for this
131      * <code>Throwable</code> object on the error output stream that is
132      * the value of the field <code>System.err</code>. The first line of
133      * output contains the result of the {@link #toString()} method for
134      * this object. Remaining lines represent data previously recorded by
135      * the method {@link #fillInStackTrace()}. The format of this
136      * information depends on the implementation, but the following
137      * example may be regarded as typical:
138      * <blockquote><pre>
139      * java.lang.NullPointerException
140      * at MyClass.mash(MyClass.java:9)
141      * at MyClass.crunch(MyClass.java:6)
142      * at MyClass.main(MyClass.java:3)
143      * </pre></blockquote>
144      * This example was produced by running the program:
145      * <blockquote><pre>
146      *
147      * class MyClass {
148      *
149      * public static void main(String[] argv) {
150      * crunch(null);
151      * }
152      * static void crunch(int[] a) {
153      * mash(a);
154      * }
155      *
156      * static void mash(int[] b) {
157      * System.out.println(b[0]);
158      * }
159      * }
160      * </pre></blockquote>
161      *
162      * @see java.lang.System#err
163      */

164     public void printStackTrace() {
165         super.printStackTrace();
166         if (m_nestedException != null) {
167             System.err.println("----- Cause Exception -----");
168             m_nestedException.printStackTrace();
169         }
170     }
171
172 }
173
Popular Tags