< prev index next >

src/java.base/share/classes/java/lang/reflect/InvocationTargetException.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 







  28 /**
  29  * InvocationTargetException is a checked exception that wraps
  30  * an exception thrown by an invoked method or constructor.
  31  *
  32  * <p>As of release 1.4, this exception has been retrofitted to conform to
  33  * the general purpose exception-chaining mechanism.  The "target exception"
  34  * that is provided at construction time and accessed via the
  35  * {@link #getTargetException()} method is now known as the <i>cause</i>,
  36  * and may be accessed via the {@link Throwable#getCause()} method,
  37  * as well as the aforementioned "legacy method."
  38  *
  39  * @see Method
  40  * @see Constructor
  41  * @since 1.1
  42  */
  43 public class InvocationTargetException extends ReflectiveOperationException {
  44     /**
  45      * Use serialVersionUID from JDK 1.1.X for interoperability
  46      */
  47     private static final long serialVersionUID = 4085088731926701167L;
  48 
  49      /**
  50      * This field holds the target if the
  51      * InvocationTargetException(Throwable target) constructor was
  52      * used to instantiate the object
  53      *
  54      * @serial
  55      *
  56      */
  57     private Throwable target;
  58 
  59     /**
  60      * Constructs an {@code InvocationTargetException} with
  61      * {@code null} as the target exception.
  62      */
  63     protected InvocationTargetException() {
  64         super((Throwable)null);  // Disallow initCause
  65     }
  66 
  67     /**
  68      * Constructs a InvocationTargetException with a target exception.
  69      *
  70      * @param target the target exception
  71      */
  72     public InvocationTargetException(Throwable target) {
  73         super((Throwable)null);  // Disallow initCause
  74         this.target = target;
  75     }
  76 
  77     /**
  78      * Constructs a InvocationTargetException with a target exception
  79      * and a detail message.
  80      *
  81      * @param target the target exception
  82      * @param s      the detail message
  83      */
  84     public InvocationTargetException(Throwable target, String s) {
  85         super(s, null);  // Disallow initCause
  86         this.target = target;
  87     }
  88 
  89     /**
  90      * Get the thrown target exception.
  91      *
  92      * <p>This method predates the general-purpose exception chaining facility.
  93      * The {@link Throwable#getCause()} method is now the preferred means of
  94      * obtaining this information.
  95      *
  96      * @return the thrown target exception (cause of this exception).
  97      */
  98     public Throwable getTargetException() {
  99         return target;
 100     }
 101 
 102     /**
 103      * Returns the cause of this exception (the thrown target exception,
 104      * which may be {@code null}).
 105      *
 106      * @return  the cause of this exception.
 107      * @since   1.4
 108      */
 109     public Throwable getCause() {
 110         return target;



























 111     }
 112 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import jdk.internal.misc.SharedSecrets;
  29 
  30 import java.io.IOException;
  31 import java.io.ObjectInputStream;
  32 import java.io.ObjectOutputStream;
  33 import java.io.ObjectStreamField;
  34 
  35 /**
  36  * InvocationTargetException is a checked exception that wraps
  37  * an exception thrown by an invoked method or constructor.
  38  *
  39  * <p>As of release 1.4, this exception has been retrofitted to conform to
  40  * the general purpose exception-chaining mechanism.  The "target exception"
  41  * that is provided at construction time and accessed via the
  42  * {@link #getTargetException()} method is now known as the <i>cause</i>,
  43  * and may be accessed via the {@link Throwable#getCause()} method,
  44  * as well as the aforementioned "legacy method."
  45  *
  46  * @see Method
  47  * @see Constructor
  48  * @since 1.1
  49  */
  50 public class InvocationTargetException extends ReflectiveOperationException {
  51     /**
  52      * Use serialVersionUID from JDK 1.1.X for interoperability
  53      */
  54     private static final long serialVersionUID = 4085088731926701167L;
  55 
  56     /**










  57      * Constructs an {@code InvocationTargetException} with
  58      * {@code null} as the target exception.
  59      */
  60     protected InvocationTargetException() {
  61         super((Throwable)null);  // Disallow initCause
  62     }
  63 
  64     /**
  65      * Constructs a InvocationTargetException with a target exception.
  66      *
  67      * @param target the target exception
  68      */
  69     public InvocationTargetException(Throwable target) {
  70         super(null, target);  // Disallow initCause

  71     }
  72 
  73     /**
  74      * Constructs a InvocationTargetException with a target exception
  75      * and a detail message.
  76      *
  77      * @param target the target exception
  78      * @param s      the detail message
  79      */
  80     public InvocationTargetException(Throwable target, String s) {
  81         super(s, target);  // Disallow initCause

  82     }
  83 
  84     /**
  85      * Get the thrown target exception.
  86      *
  87      * <p>This method predates the general-purpose exception chaining facility.
  88      * The {@link Throwable#getCause()} method is now the preferred means of
  89      * obtaining this information.
  90      *
  91      * @return the thrown target exception (cause of this exception).
  92      */
  93     public Throwable getTargetException() {
  94         return super.getCause();
  95     }
  96 
  97     /**
  98      * Serializable fields for UndeclaredThrowableException.

  99      *
 100      * @serialField target Throwable

 101      */
 102     private static final ObjectStreamField[] serialPersistentFields = {
 103         new ObjectStreamField("target", Throwable.class)
 104     };
 105 
 106     /*
 107      * Reconstitutes the InvocationTargetException instance from a stream
 108      * and initialize the cause properly when deserializing from an older
 109      * version.
 110      *
 111      * The getException and getCause method returns the private "target" field
 112      * in the older implementation and InvocationTargetException::cause
 113      * was set to null.
 114      */
 115     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
 116         ObjectInputStream.GetField fields = s.readFields();
 117         Throwable exception = (Throwable) fields.get("target", null);
 118         if (exception != null) {
 119             SharedSecrets.getJavaLangAccess().setCause(this, exception);
 120         }
 121     }
 122 
 123     /*
 124      * To maintain compatibility with older implementation, write a serial
 125      * "target" field with the cause as the value.
 126      */
 127     private void writeObject(ObjectOutputStream out) throws IOException {
 128         ObjectOutputStream.PutField fields = out.putFields();
 129         fields.put("target", super.getCause());
 130         out.writeFields();
 131     }
 132 }
< prev index next >