1 /*
2  * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
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  */
42 public class InvocationTargetException extends ReflectiveOperationException {
43     /**
44      * Use serialVersionUID from JDK 1.1.X for interoperability
45      */
46     private static final long serialVersionUID = 4085088731926701167L;
47 
48      /**
49      * This field holds the target if the
50      * InvocationTargetException(Throwable target) constructor was
51      * used to instantiate the object
52      *
53      * @serial
54      *
55      */
56     private Throwable target;
57 
58     /**
59      * Constructs an {@code InvocationTargetException} with
60      * {@code null} as the target exception.
61      */
InvocationTargetException()62     protected InvocationTargetException() {
63         super((Throwable)null);  // Disallow initCause
64     }
65 
66     /**
67      * Constructs a InvocationTargetException with a target exception.
68      *
69      * @param target the target exception
70      */
InvocationTargetException(Throwable target)71     public InvocationTargetException(Throwable target) {
72         super((Throwable)null);  // Disallow initCause
73         this.target = target;
74     }
75 
76     /**
77      * Constructs a InvocationTargetException with a target exception
78      * and a detail message.
79      *
80      * @param target the target exception
81      * @param s      the detail message
82      */
InvocationTargetException(Throwable target, String s)83     public InvocationTargetException(Throwable target, String s) {
84         super(s, null);  // Disallow initCause
85         this.target = target;
86     }
87 
88     /**
89      * Get the thrown target exception.
90      *
91      * <p>This method predates the general-purpose exception chaining facility.
92      * The {@link Throwable#getCause()} method is now the preferred means of
93      * obtaining this information.
94      *
95      * @return the thrown target exception (cause of this exception).
96      */
getTargetException()97     public Throwable getTargetException() {
98         return target;
99     }
100 
101     /**
102      * Returns the cause of this exception (the thrown target exception,
103      * which may be {@code null}).
104      *
105      * @return  the cause of this exception.
106      * @since   1.4
107      */
getCause()108     public Throwable getCause() {
109         return target;
110     }
111 }
112