1 | /*
|
---|
2 | * Copyright 2000-2004 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.tools.ant;
|
---|
18 |
|
---|
19 | import java.io.PrintStream;
|
---|
20 | import java.io.PrintWriter;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Signals an error condition during a build
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class BuildException extends RuntimeException {
|
---|
27 |
|
---|
28 | /** Exception that might have caused this one. */
|
---|
29 | private Throwable 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 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 message, Throwable 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 msg, Throwable 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 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 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 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 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 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 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 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 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 | }
|
---|