KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > BuildException


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant;
19
20 import java.io.PrintStream JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22
23 /**
24  * Signals an error condition during a build
25  */

26 public class BuildException extends RuntimeException JavaDoc {
27
28     /** Exception that might have caused this one. */
29     private Throwable JavaDoc cause;
30
31     /** Location in the build file where the exception occurred */
32     private Location location = Location.UNKNOWN_LOCATION;
33
34     /**
35      * Constructs a build exception with no descriptive information.
36      */

37     public BuildException() {
38         super();
39     }
40
41     /**
42      * Constructs an exception with the given descriptive message.
43      *
44      * @param message A description of or information about the exception.
45      * Should not be <code>null</code>.
46      */

47     public BuildException(String JavaDoc message) {
48         super(message);
49     }
50
51     /**
52      * Constructs an exception with the given message and exception as
53      * a root cause.
54      *
55      * @param message A description of or information about the exception.
56      * Should not be <code>null</code> unless a cause is specified.
57      * @param cause The exception that might have caused this one.
58      * May be <code>null</code>.
59      */

60     public BuildException(String JavaDoc message, Throwable JavaDoc cause) {
61         super(message);
62         this.cause = cause;
63     }
64
65     /**
66      * Constructs an exception with the given message and exception as
67      * a root cause and a location in a file.
68      *
69      * @param msg A description of or information about the exception.
70      * Should not be <code>null</code> unless a cause is specified.
71      * @param cause The exception that might have caused this one.
72      * May be <code>null</code>.
73      * @param location The location in the project file where the error
74      * occurred. Must not be <code>null</code>.
75      */

76     public BuildException(String JavaDoc msg, Throwable JavaDoc cause, Location location) {
77         this(msg, cause);
78         this.location = location;
79     }
80
81     /**
82      * Constructs an exception with the given exception as a root cause.
83      *
84      * @param cause The exception that might have caused this one.
85      * Should not be <code>null</code>.
86      */

87     public BuildException(Throwable JavaDoc cause) {
88         super(cause.toString());
89         this.cause = cause;
90     }
91
92     /**
93      * Constructs an exception with the given descriptive message and a
94      * location in a file.
95      *
96      * @param message A description of or information about the exception.
97      * Should not be <code>null</code>.
98      * @param location The location in the project file where the error
99      * occurred. Must not be <code>null</code>.
100      */

101     public BuildException(String JavaDoc message, Location location) {
102         super(message);
103         this.location = location;
104     }
105
106     /**
107      * Constructs an exception with the given exception as
108      * a root cause and a location in a file.
109      *
110      * @param cause The exception that might have caused this one.
111      * Should not be <code>null</code>.
112      * @param location The location in the project file where the error
113      * occurred. Must not be <code>null</code>.
114      */

115     public BuildException(Throwable JavaDoc cause, Location location) {
116         this(cause);
117         this.location = location;
118     }
119
120     /**
121      * Returns the nested exception, if any.
122      *
123      * @return the nested exception, or <code>null</code> if no
124      * exception is associated with this one
125      */

126     public Throwable JavaDoc getException() {
127         return cause;
128     }
129
130     /**
131      * Returns the nested exception, if any.
132      *
133      * @return the nested exception, or <code>null</code> if no
134      * exception is associated with this one
135      */

136     public Throwable JavaDoc getCause() {
137         return getException();
138     }
139
140     /**
141      * Returns the location of the error and the error message.
142      *
143      * @return the location of the error and the error message
144      */

145     public String JavaDoc toString() {
146         return location.toString() + getMessage();
147     }
148
149     /**
150      * Sets the file location where the error occurred.
151      *
152      * @param location The file location where the error occurred.
153      * Must not be <code>null</code>.
154      */

155     public void setLocation(Location location) {
156         this.location = location;
157     }
158
159     /**
160      * Returns the file location where the error occurred.
161      *
162      * @return the file location where the error occurred.
163      */

164     public Location getLocation() {
165         return location;
166     }
167
168     /**
169      * Prints the stack trace for this exception and any
170      * nested exception to <code>System.err</code>.
171      */

172     public void printStackTrace() {
173         printStackTrace(System.err);
174     }
175
176     /**
177      * Prints the stack trace of this exception and any nested
178      * exception to the specified PrintStream.
179      *
180      * @param ps The PrintStream to print the stack trace to.
181      * Must not be <code>null</code>.
182      */

183     public void printStackTrace(PrintStream JavaDoc ps) {
184         synchronized (ps) {
185             super.printStackTrace(ps);
186             if (cause != null) {
187                 ps.println("--- Nested Exception ---");
188                 cause.printStackTrace(ps);
189             }
190         }
191     }
192
193     /**
194      * Prints the stack trace of this exception and any nested
195      * exception to the specified PrintWriter.
196      *
197      * @param pw The PrintWriter to print the stack trace to.
198      * Must not be <code>null</code>.
199      */

200     public void printStackTrace(PrintWriter JavaDoc pw) {
201         synchronized (pw) {
202             super.printStackTrace(pw);
203             if (cause != null) {
204                 pw.println("--- Nested Exception ---");
205                 cause.printStackTrace(pw);
206             }
207         }
208     }
209 }
210
Popular Tags