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. import java.io.PrintStream;
  19. import java.io.PrintWriter;
  20. /**
  21. * Signals an error condition during a build
  22. *
  23. */
  24. public class BuildException extends RuntimeException {
  25. /** Exception that might have caused this one. */
  26. private Throwable cause;
  27. /** Location in the build file where the exception occurred */
  28. private Location location = Location.UNKNOWN_LOCATION;
  29. /**
  30. * Constructs a build exception with no descriptive information.
  31. */
  32. public BuildException() {
  33. super();
  34. }
  35. /**
  36. * Constructs an exception with the given descriptive message.
  37. *
  38. * @param message A description of or information about the exception.
  39. * Should not be <code>null</code>.
  40. */
  41. public BuildException(String message) {
  42. super(message);
  43. }
  44. /**
  45. * Constructs an exception with the given message and exception as
  46. * a root cause.
  47. *
  48. * @param message A description of or information about the exception.
  49. * Should not be <code>null</code> unless a cause is specified.
  50. * @param cause The exception that might have caused this one.
  51. * May be <code>null</code>.
  52. */
  53. public BuildException(String message, Throwable cause) {
  54. super(message);
  55. this.cause = cause;
  56. }
  57. /**
  58. * Constructs an exception with the given message and exception as
  59. * a root cause and a location in a file.
  60. *
  61. * @param msg A description of or information about the exception.
  62. * Should not be <code>null</code> unless a cause is specified.
  63. * @param cause The exception that might have caused this one.
  64. * May be <code>null</code>.
  65. * @param location The location in the project file where the error
  66. * occurred. Must not be <code>null</code>.
  67. */
  68. public BuildException(String msg, Throwable cause, Location location) {
  69. this(msg, cause);
  70. this.location = location;
  71. }
  72. /**
  73. * Constructs an exception with the given exception as a root cause.
  74. *
  75. * @param cause The exception that might have caused this one.
  76. * Should not be <code>null</code>.
  77. */
  78. public BuildException(Throwable cause) {
  79. super(cause.toString());
  80. this.cause = cause;
  81. }
  82. /**
  83. * Constructs an exception with the given descriptive message and a
  84. * location in a file.
  85. *
  86. * @param message A description of or information about the exception.
  87. * Should not be <code>null</code>.
  88. * @param location The location in the project file where the error
  89. * occurred. Must not be <code>null</code>.
  90. */
  91. public BuildException(String message, Location location) {
  92. super(message);
  93. this.location = location;
  94. }
  95. /**
  96. * Constructs an exception with the given exception as
  97. * a root cause and a location in a file.
  98. *
  99. * @param cause The exception that might have caused this one.
  100. * Should not be <code>null</code>.
  101. * @param location The location in the project file where the error
  102. * occurred. Must not be <code>null</code>.
  103. */
  104. public BuildException(Throwable cause, Location location) {
  105. this(cause);
  106. this.location = location;
  107. }
  108. /**
  109. * Returns the nested exception, if any.
  110. *
  111. * @return the nested exception, or <code>null</code> if no
  112. * exception is associated with this one
  113. */
  114. public Throwable getException() {
  115. return cause;
  116. }
  117. /**
  118. * Returns the nested exception, if any.
  119. *
  120. * @return the nested exception, or <code>null</code> if no
  121. * exception is associated with this one
  122. */
  123. public Throwable getCause() {
  124. return getException();
  125. }
  126. /**
  127. * Returns the location of the error and the error message.
  128. *
  129. * @return the location of the error and the error message
  130. */
  131. public String toString() {
  132. return location.toString() + getMessage();
  133. }
  134. /**
  135. * Sets the file location where the error occurred.
  136. *
  137. * @param location The file location where the error occurred.
  138. * Must not be <code>null</code>.
  139. */
  140. public void setLocation(Location location) {
  141. this.location = location;
  142. }
  143. /**
  144. * Returns the file location where the error occurred.
  145. *
  146. * @return the file location where the error occurred.
  147. */
  148. public Location getLocation() {
  149. return location;
  150. }
  151. /**
  152. * Prints the stack trace for this exception and any
  153. * nested exception to <code>System.err</code>.
  154. */
  155. public void printStackTrace() {
  156. printStackTrace(System.err);
  157. }
  158. /**
  159. * Prints the stack trace of this exception and any nested
  160. * exception to the specified PrintStream.
  161. *
  162. * @param ps The PrintStream to print the stack trace to.
  163. * Must not be <code>null</code>.
  164. */
  165. public void printStackTrace(PrintStream ps) {
  166. synchronized (ps) {
  167. super.printStackTrace(ps);
  168. if (cause != null) {
  169. ps.println("--- Nested Exception ---");
  170. cause.printStackTrace(ps);
  171. }
  172. }
  173. }
  174. /**
  175. * Prints the stack trace of this exception and any nested
  176. * exception to the specified PrintWriter.
  177. *
  178. * @param pw The PrintWriter to print the stack trace to.
  179. * Must not be <code>null</code>.
  180. */
  181. public void printStackTrace(PrintWriter pw) {
  182. synchronized (pw) {
  183. super.printStackTrace(pw);
  184. if (cause != null) {
  185. pw.println("--- Nested Exception ---");
  186. cause.printStackTrace(pw);
  187. }
  188. }
  189. }
  190. }