30 |
import java.io.PrintStream; |
import java.io.PrintStream; |
31 |
import java.io.PrintWriter; |
import java.io.PrintWriter; |
32 |
|
|
|
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 |
|
|
* "The Java Language Specification", ISBN 0-201-63451-1 |
|
|
* Status: Believed complete and correct. |
|
|
*/ |
|
|
|
|
33 |
/** |
/** |
34 |
* InvocationTargetException is sort of a way to "wrap" whatever exception |
* InvocationTargetException is sort of a way to "wrap" whatever exception |
35 |
* comes up when a method or constructor is called via Reflection. |
* comes up when a method or constructor is called via Reflection. As of |
36 |
|
* JDK 1.4, it was retrofitted to match the exception chaining of all other |
37 |
|
* exceptions, but <code>getTargetException()</code> still works. |
38 |
* |
* |
39 |
* @author John Keiser |
* @author John Keiser |
|
* @version 1.1.0, 31 May 1998 |
|
40 |
* @author Tom Tromey <tromey@cygnus.com> |
* @author Tom Tromey <tromey@cygnus.com> |
41 |
* @date December 12, 1998 |
* @author Eric Blake <ebb9@email.byu.edu> |
|
* |
|
42 |
* @see Method#invoke(Object,Object[]) |
* @see Method#invoke(Object,Object[]) |
43 |
* @see Constructor#newInstance(Object[]) |
* @see Constructor#newInstance(Object[]) |
44 |
|
* @since 1.1 |
45 |
|
* @status stuck at 1.3; awaiting exception chaining in Throwable |
46 |
*/ |
*/ |
47 |
|
|
48 |
public class InvocationTargetException extends Exception |
public class InvocationTargetException |
49 |
|
extends Exception |
50 |
{ |
{ |
51 |
static final long serialVersionUID = 4085088731926701167L; |
/** |
52 |
|
* Compatible with JDK 1.1. |
53 |
|
*/ |
54 |
|
private static final long serialVersionUID = 4085088731926701167L; |
55 |
|
|
56 |
private Throwable target = null; |
/** |
57 |
|
* The chained exception. This field is only around for serial compatibility. |
58 |
|
* @serial the chained exception |
59 |
|
*/ |
60 |
|
private final Throwable target; |
61 |
|
|
62 |
|
/** |
63 |
|
* Construct an exception with null as the cause. |
64 |
|
*/ |
65 |
protected InvocationTargetException() |
protected InvocationTargetException() |
66 |
{ |
{ |
67 |
super(); |
// XXX Use this implementation when Throwable is ready. |
68 |
} |
// this(null); |
69 |
|
|
70 |
|
super(); |
71 |
|
target = null; |
72 |
|
} |
73 |
|
|
74 |
/** |
/** |
75 |
* Create an <code>InvocationTargetException</code> using another |
* Create an <code>InvocationTargetException</code> using another |
76 |
* exception. |
* exception. |
77 |
|
* |
78 |
* @param targetException the exception to wrap |
* @param targetException the exception to wrap |
79 |
*/ |
*/ |
80 |
public InvocationTargetException(Throwable targetException) |
public InvocationTargetException(Throwable targetException) |
81 |
{ |
{ |
82 |
super(targetException.toString()); |
// XXX Use this implementation when Throwable is ready. |
83 |
target = targetException; |
// super(targetException); |
84 |
} |
// target = targetException; |
85 |
|
|
86 |
|
super(targetException.toString()); |
87 |
|
target = targetException; |
88 |
|
} |
89 |
|
|
90 |
/** |
/** |
91 |
* Create an <code>InvocationTargetException</code> using another |
* Create an <code>InvocationTargetException</code> using another |
95 |
* @param err an extra reason for the exception-throwing |
* @param err an extra reason for the exception-throwing |
96 |
*/ |
*/ |
97 |
public InvocationTargetException(Throwable targetException, String err) |
public InvocationTargetException(Throwable targetException, String err) |
98 |
{ |
{ |
99 |
super(err); |
// XXX Use this implementation when Throwable is ready. |
100 |
target = targetException; |
// super(err, targetException); |
101 |
} |
// target = targetException; |
102 |
|
|
103 |
|
super(err); |
104 |
|
target = targetException; |
105 |
|
} |
106 |
|
|
107 |
/** |
/** |
108 |
* Get the wrapped (targeted) exception. |
* Get the wrapped (targeted) exception. |
109 |
* |
* |
110 |
* @return the targeted exception. |
* @return the targeted exception. |
111 |
|
* @see #getCause() |
112 |
*/ |
*/ |
113 |
public Throwable getTargetException() |
public Throwable getTargetException() |
114 |
{ |
{ |
115 |
return target; |
return target; |
116 |
} |
} |
117 |
|
|
118 |
public void printStackTrace() |
/** |
119 |
{ |
* Returns the cause of this exception (which may be null). |
120 |
if (target == null) |
* |
121 |
super.printStackTrace(); |
* @return the cause. |
122 |
else |
* @since 1.4 |
123 |
{ |
*/ |
124 |
System.err.print(this.getClass() + ": "); |
public Throwable getCause() |
125 |
target.printStackTrace(); |
{ |
126 |
} |
return target; |
127 |
} |
} |
|
|
|
|
public void printStackTrace(PrintStream ps) |
|
|
{ |
|
|
if (target == null) |
|
|
super.printStackTrace(ps); |
|
|
else |
|
|
{ |
|
|
ps.print(this.getClass() + ": "); |
|
|
target.printStackTrace(ps); |
|
|
} |
|
|
} |
|
|
|
|
|
public void printStackTrace(PrintWriter pw) |
|
|
{ |
|
|
if (target == null) |
|
|
super.printStackTrace(pw); |
|
|
else |
|
|
{ |
|
|
pw.print(this.getClass() + ": "); |
|
|
target.printStackTrace(pw); |
|
|
} |
|
|
} |
|
128 |
} |
} |