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 java.lang.reflect;
19 
20 /**
21  * This class provides a wrapper for an exception thrown by a {@code Method} or
22  * {@code Constructor} invocation.
23  *
24  * @see Method#invoke
25  * @see Constructor#newInstance
26  */
27 public class InvocationTargetException extends ReflectiveOperationException {
28 
29     private static final long serialVersionUID = 4085088731926701167L;
30 
31     private Throwable target;
32 
33     /**
34      * Constructs a new {@code InvocationTargetException} instance with a
35      * {@code null} cause / target exception.
36      */
InvocationTargetException()37     protected InvocationTargetException() {
38         super((Throwable) null);
39     }
40 
41     /**
42      * Constructs a new {@code InvocationTargetException} instance with its
43      * cause / target exception filled in.
44      *
45      * @param exception
46      *            the exception which occurred while running the Method or
47      *            Constructor
48      */
InvocationTargetException(Throwable exception)49     public InvocationTargetException(Throwable exception) {
50         super(null, exception);
51         target = exception;
52     }
53 
54     /**
55      * Constructs a new {@code InvocationTargetException} instance with its
56      * cause / target exception and message filled in.
57      *
58      * @param detailMessage
59      *            the detail message for the exception
60      * @param exception
61      *            the exception which occurred while running the Method or
62      *            Constructor
63      */
InvocationTargetException(Throwable exception, String detailMessage)64     public InvocationTargetException(Throwable exception, String detailMessage) {
65         super(detailMessage, exception);
66         target = exception;
67     }
68 
69     /**
70      * Returns the target exception, which may be {@code null}.
71      *
72      * @return the target exception
73      */
getTargetException()74     public Throwable getTargetException() {
75         return target;
76     }
77 
78     /**
79      * Returns the cause of this exception, which may be {@code null}.
80      *
81      * @return the cause of this exception
82      */
83     @Override
getCause()84     public Throwable getCause() {
85         return target;
86     }
87 }
88