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

Save This Page
Home » apache-ant-1.7.1-src » org.apache.tools » ant » [javadoc | source]