You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuildException.java 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import java.io.PrintWriter;
  56. import java.io.PrintStream;
  57. /**
  58. * Signals an error condition during a build
  59. *
  60. * @author James Duncan Davidson
  61. */
  62. public class BuildException extends RuntimeException {
  63. /** Exception that might have caused this one. */
  64. private Throwable cause;
  65. /** Location in the build file where the exception occured */
  66. private Location location = Location.UNKNOWN_LOCATION;
  67. /**
  68. * Constructs a build exception with no descriptive information.
  69. */
  70. public BuildException() {
  71. super();
  72. }
  73. /**
  74. * Constructs an exception with the given descriptive message.
  75. *
  76. * @param msg A description of or information about the exception.
  77. * Should not be <code>null</code>.
  78. */
  79. public BuildException(String msg) {
  80. super(msg);
  81. }
  82. /**
  83. * Constructs an exception with the given message and exception as
  84. * a root cause.
  85. *
  86. * @param msg A description of or information about the exception.
  87. * Should not be <code>null</code> unless a cause is specified.
  88. * @param cause The exception that might have caused this one.
  89. * May be <code>null</code>.
  90. */
  91. public BuildException(String msg, Throwable cause) {
  92. super(msg);
  93. this.cause = cause;
  94. }
  95. /**
  96. * Constructs an exception with the given message and exception as
  97. * a root cause and a location in a file.
  98. *
  99. * @param msg A description of or information about the exception.
  100. * Should not be <code>null</code> unless a cause is specified.
  101. * @param cause The exception that might have caused this one.
  102. * May be <code>null</code>.
  103. * @param location The location in the project file where the error
  104. * occurred. Must not be <code>null</code>.
  105. */
  106. public BuildException(String msg, Throwable cause, Location location) {
  107. this(msg, cause);
  108. this.location = location;
  109. }
  110. /**
  111. * Constructs an exception with the given exception as a root cause.
  112. *
  113. * @param cause The exception that might have caused this one.
  114. * Should not be <code>null</code>.
  115. */
  116. public BuildException(Throwable cause) {
  117. super(cause.toString());
  118. this.cause = cause;
  119. }
  120. /**
  121. * Constructs an exception with the given descriptive message and a
  122. * location in a file.
  123. *
  124. * @param msg A description of or information about the exception.
  125. * Should not be <code>null</code>.
  126. * @param location The location in the project file where the error
  127. * occurred. Must not be <code>null</code>.
  128. */
  129. public BuildException(String msg, Location location) {
  130. super(msg);
  131. this.location = location;
  132. }
  133. /**
  134. * Constructs an exception with the given exception as
  135. * a root cause and a location in a file.
  136. *
  137. * @param cause The exception that might have caused this one.
  138. * Should not be <code>null</code>.
  139. * @param location The location in the project file where the error
  140. * occurred. Must not be <code>null</code>.
  141. */
  142. public BuildException(Throwable cause, Location location) {
  143. this(cause);
  144. this.location = location;
  145. }
  146. /**
  147. * Returns the nested exception, if any.
  148. *
  149. * @return the nested exception, or <code>null</code> if no
  150. * exception is associated with this one
  151. */
  152. public Throwable getException() {
  153. return cause;
  154. }
  155. /**
  156. * Returns the location of the error and the error message.
  157. *
  158. * @return the location of the error and the error message
  159. */
  160. public String toString() {
  161. return location.toString() + getMessage();
  162. }
  163. /**
  164. * Sets the file location where the error occurred.
  165. *
  166. * @param location The file location where the error occurred.
  167. * Must not be <code>null</code>.
  168. */
  169. public void setLocation(Location location) {
  170. this.location = location;
  171. }
  172. /**
  173. * Returns the file location where the error occurred.
  174. *
  175. * @return the file location where the error occurred.
  176. */
  177. public Location getLocation() {
  178. return location;
  179. }
  180. /**
  181. * Prints the stack trace for this exception and any
  182. * nested exception to <code>System.err</code>.
  183. */
  184. public void printStackTrace() {
  185. printStackTrace(System.err);
  186. }
  187. /**
  188. * Prints the stack trace of this exception and any nested
  189. * exception to the specified PrintStream.
  190. *
  191. * @param ps The PrintStream to print the stack trace to.
  192. * Must not be <code>null</code>.
  193. */
  194. public void printStackTrace(PrintStream ps) {
  195. synchronized (ps) {
  196. super.printStackTrace(ps);
  197. if (cause != null) {
  198. ps.println("--- Nested Exception ---");
  199. cause.printStackTrace(ps);
  200. }
  201. }
  202. }
  203. /**
  204. * Prints the stack trace of this exception and any nested
  205. * exception to the specified PrintWriter.
  206. *
  207. * @param pw The PrintWriter to print the stack trace to.
  208. * Must not be <code>null</code>.
  209. */
  210. public void printStackTrace(PrintWriter pw) {
  211. synchronized (pw) {
  212. super.printStackTrace(pw);
  213. if (cause != null) {
  214. pw.println("--- Nested Exception ---");
  215. cause.printStackTrace(pw);
  216. }
  217. }
  218. }
  219. }