author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 28059 | e576535359cc |
child 33313 | de27000120de |
permissions | -rw-r--r-- |
2 | 1 |
/* |
17176
d7ee1e315fe7
8012044: Give more information about self-suppression from Throwable.addSuppressed
darcy
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
import java.io.*; |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
28 |
import java.util.*; |
2 | 29 |
|
30 |
/** |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
31 |
* The {@code Throwable} class is the superclass of all errors and |
2 | 32 |
* exceptions in the Java language. Only objects that are instances of this |
33 |
* class (or one of its subclasses) are thrown by the Java Virtual Machine or |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
34 |
* can be thrown by the Java {@code throw} statement. Similarly, only |
2 | 35 |
* this class or one of its subclasses can be the argument type in a |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
36 |
* {@code catch} clause. |
2 | 37 |
* |
4666
085aef3c09ff
6915171: Clarify checked/unchecked status of Throwable and its subclasses
darcy
parents:
2947
diff
changeset
|
38 |
* For the purposes of compile-time checking of exceptions, {@code |
085aef3c09ff
6915171: Clarify checked/unchecked status of Throwable and its subclasses
darcy
parents:
2947
diff
changeset
|
39 |
* Throwable} and any subclass of {@code Throwable} that is not also a |
085aef3c09ff
6915171: Clarify checked/unchecked status of Throwable and its subclasses
darcy
parents:
2947
diff
changeset
|
40 |
* subclass of either {@link RuntimeException} or {@link Error} are |
085aef3c09ff
6915171: Clarify checked/unchecked status of Throwable and its subclasses
darcy
parents:
2947
diff
changeset
|
41 |
* regarded as checked exceptions. |
085aef3c09ff
6915171: Clarify checked/unchecked status of Throwable and its subclasses
darcy
parents:
2947
diff
changeset
|
42 |
* |
2 | 43 |
* <p>Instances of two subclasses, {@link java.lang.Error} and |
44 |
* {@link java.lang.Exception}, are conventionally used to indicate |
|
45 |
* that exceptional situations have occurred. Typically, these instances |
|
46 |
* are freshly created in the context of the exceptional situation so |
|
47 |
* as to include relevant information (such as stack trace data). |
|
48 |
* |
|
9005
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
49 |
* <p>A throwable contains a snapshot of the execution stack of its |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
50 |
* thread at the time it was created. It can also contain a message |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
51 |
* string that gives more information about the error. Over time, a |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
52 |
* throwable can {@linkplain Throwable#addSuppressed suppress} other |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
53 |
* throwables from being propagated. Finally, the throwable can also |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
54 |
* contain a <i>cause</i>: another throwable that caused this |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
55 |
* throwable to be constructed. The recording of this causal information |
9005
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
56 |
* is referred to as the <i>chained exception</i> facility, as the |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
57 |
* cause can, itself, have a cause, and so on, leading to a "chain" of |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
58 |
* exceptions, each caused by another. |
2 | 59 |
* |
60 |
* <p>One reason that a throwable may have a cause is that the class that |
|
61 |
* throws it is built atop a lower layered abstraction, and an operation on |
|
62 |
* the upper layer fails due to a failure in the lower layer. It would be bad |
|
63 |
* design to let the throwable thrown by the lower layer propagate outward, as |
|
64 |
* it is generally unrelated to the abstraction provided by the upper layer. |
|
65 |
* Further, doing so would tie the API of the upper layer to the details of |
|
66 |
* its implementation, assuming the lower layer's exception was a checked |
|
67 |
* exception. Throwing a "wrapped exception" (i.e., an exception containing a |
|
68 |
* cause) allows the upper layer to communicate the details of the failure to |
|
69 |
* its caller without incurring either of these shortcomings. It preserves |
|
70 |
* the flexibility to change the implementation of the upper layer without |
|
71 |
* changing its API (in particular, the set of exceptions thrown by its |
|
72 |
* methods). |
|
73 |
* |
|
74 |
* <p>A second reason that a throwable may have a cause is that the method |
|
75 |
* that throws it must conform to a general-purpose interface that does not |
|
76 |
* permit the method to throw the cause directly. For example, suppose |
|
77 |
* a persistent collection conforms to the {@link java.util.Collection |
|
78 |
* Collection} interface, and that its persistence is implemented atop |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
79 |
* {@code java.io}. Suppose the internals of the {@code add} method |
2 | 80 |
* can throw an {@link java.io.IOException IOException}. The implementation |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
81 |
* can communicate the details of the {@code IOException} to its caller |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
82 |
* while conforming to the {@code Collection} interface by wrapping the |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
83 |
* {@code IOException} in an appropriate unchecked exception. (The |
2 | 84 |
* specification for the persistent collection should indicate that it is |
85 |
* capable of throwing such exceptions.) |
|
86 |
* |
|
87 |
* <p>A cause can be associated with a throwable in two ways: via a |
|
88 |
* constructor that takes the cause as an argument, or via the |
|
89 |
* {@link #initCause(Throwable)} method. New throwable classes that |
|
90 |
* wish to allow causes to be associated with them should provide constructors |
|
91 |
* that take a cause and delegate (perhaps indirectly) to one of the |
|
9005
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
92 |
* {@code Throwable} constructors that takes a cause. |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
93 |
* |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
94 |
* Because the {@code initCause} method is public, it allows a cause to be |
2 | 95 |
* associated with any throwable, even a "legacy throwable" whose |
96 |
* implementation predates the addition of the exception chaining mechanism to |
|
9005
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
97 |
* {@code Throwable}. |
2 | 98 |
* |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
99 |
* <p>By convention, class {@code Throwable} and its subclasses have two |
2 | 100 |
* constructors, one that takes no arguments and one that takes a |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
101 |
* {@code String} argument that can be used to produce a detail message. |
2 | 102 |
* Further, those subclasses that might likely have a cause associated with |
103 |
* them should have two more constructors, one that takes a |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
104 |
* {@code Throwable} (the cause), and one that takes a |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
105 |
* {@code String} (the detail message) and a {@code Throwable} (the |
2 | 106 |
* cause). |
107 |
* |
|
108 |
* @author unascribed |
|
109 |
* @author Josh Bloch (Added exception chaining and programmatic access to |
|
110 |
* stack trace in 1.4.) |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
9020
diff
changeset
|
111 |
* @jls 11.2 Compile-Time Checking of Exceptions |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
22581
diff
changeset
|
112 |
* @since 1.0 |
2 | 113 |
*/ |
114 |
public class Throwable implements Serializable { |
|
115 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
|
116 |
private static final long serialVersionUID = -3042686055658047285L; |
|
117 |
||
118 |
/** |
|
119 |
* Native code saves some indication of the stack backtrace in this slot. |
|
120 |
*/ |
|
121 |
private transient Object backtrace; |
|
122 |
||
123 |
/** |
|
124 |
* Specific details about the Throwable. For example, for |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
125 |
* {@code FileNotFoundException}, this contains the name of |
2 | 126 |
* the file that could not be found. |
127 |
* |
|
128 |
* @serial |
|
129 |
*/ |
|
130 |
private String detailMessage; |
|
131 |
||
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
132 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
133 |
/** |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
134 |
* Holder class to defer initializing sentinel objects only used |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
135 |
* for serialization. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
136 |
*/ |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
137 |
private static class SentinelHolder { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
138 |
/** |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
139 |
* {@linkplain #setStackTrace(StackTraceElement[]) Setting the |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
140 |
* stack trace} to a one-element array containing this sentinel |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
141 |
* value indicates future attempts to set the stack trace will be |
25979 | 142 |
* ignored. The sentinel is equal to the result of calling:<br> |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
143 |
* {@code new StackTraceElement("", "", null, Integer.MIN_VALUE)} |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
144 |
*/ |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
145 |
public static final StackTraceElement STACK_TRACE_ELEMENT_SENTINEL = |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
146 |
new StackTraceElement("", "", null, Integer.MIN_VALUE); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
147 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
148 |
/** |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
149 |
* Sentinel value used in the serial form to indicate an immutable |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
150 |
* stack trace. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
151 |
*/ |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
152 |
public static final StackTraceElement[] STACK_TRACE_SENTINEL = |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
153 |
new StackTraceElement[] {STACK_TRACE_ELEMENT_SENTINEL}; |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
154 |
} |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
155 |
|
2 | 156 |
/** |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
157 |
* A shared value for an empty stack. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
158 |
*/ |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
159 |
private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0]; |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
160 |
|
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
161 |
/* |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
162 |
* To allow Throwable objects to be made immutable and safely |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
163 |
* reused by the JVM, such as OutOfMemoryErrors, fields of |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
164 |
* Throwable that are writable in response to user actions, cause, |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
165 |
* stackTrace, and suppressedExceptions obey the following |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
166 |
* protocol: |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
167 |
* |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
168 |
* 1) The fields are initialized to a non-null sentinel value |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
169 |
* which indicates the value has logically not been set. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
170 |
* |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
171 |
* 2) Writing a null to the field indicates further writes |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
172 |
* are forbidden |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
173 |
* |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
174 |
* 3) The sentinel value may be replaced with another non-null |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
175 |
* value. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
176 |
* |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
177 |
* For example, implementations of the HotSpot JVM have |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
178 |
* preallocated OutOfMemoryError objects to provide for better |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
179 |
* diagnosability of that situation. These objects are created |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
180 |
* without calling the constructor for that class and the fields |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
181 |
* in question are initialized to null. To support this |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
182 |
* capability, any new fields added to Throwable that require |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
183 |
* being initialized to a non-null value require a coordinated JVM |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
184 |
* change. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
185 |
*/ |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
186 |
|
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
187 |
/** |
2 | 188 |
* The throwable that caused this throwable to get thrown, or null if this |
189 |
* throwable was not caused by another throwable, or if the causative |
|
190 |
* throwable is unknown. If this field is equal to this throwable itself, |
|
191 |
* it indicates that the cause of this throwable has not yet been |
|
192 |
* initialized. |
|
193 |
* |
|
194 |
* @serial |
|
195 |
* @since 1.4 |
|
196 |
*/ |
|
197 |
private Throwable cause = this; |
|
198 |
||
199 |
/** |
|
200 |
* The stack trace, as returned by {@link #getStackTrace()}. |
|
201 |
* |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
202 |
* The field is initialized to a zero-length array. A {@code |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
203 |
* null} value of this field indicates subsequent calls to {@link |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
204 |
* #setStackTrace(StackTraceElement[])} and {@link |
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25991
diff
changeset
|
205 |
* #fillInStackTrace()} will be no-ops. |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
206 |
* |
2 | 207 |
* @serial |
208 |
* @since 1.4 |
|
209 |
*/ |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
210 |
private StackTraceElement[] stackTrace = UNASSIGNED_STACK; |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
211 |
|
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
212 |
// Setting this static field introduces an acceptable |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
213 |
// initialization dependency on a few java.util classes. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
214 |
private static final List<Throwable> SUPPRESSED_SENTINEL = |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
215 |
Collections.unmodifiableList(new ArrayList<Throwable>(0)); |
2 | 216 |
|
217 |
/** |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
218 |
* The list of suppressed exceptions, as returned by {@link |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
219 |
* #getSuppressed()}. The list is initialized to a zero-element |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
220 |
* unmodifiable sentinel list. When a serialized Throwable is |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
221 |
* read in, if the {@code suppressedExceptions} field points to a |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
222 |
* zero-element list, the field is reset to the sentinel value. |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
223 |
* |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
224 |
* @serial |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
225 |
* @since 1.7 |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
226 |
*/ |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
227 |
private List<Throwable> suppressedExceptions = SUPPRESSED_SENTINEL; |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
228 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
229 |
/** Message for trying to suppress a null exception. */ |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
230 |
private static final String NULL_CAUSE_MESSAGE = "Cannot suppress a null exception."; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
231 |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
232 |
/** Message for trying to suppress oneself. */ |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
233 |
private static final String SELF_SUPPRESSION_MESSAGE = "Self-suppression not permitted"; |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
234 |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
235 |
/** Caption for labeling causative exception stack traces */ |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
236 |
private static final String CAUSE_CAPTION = "Caused by: "; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
237 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
238 |
/** Caption for labeling suppressed exception stack traces */ |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
239 |
private static final String SUPPRESSED_CAPTION = "Suppressed: "; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
240 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
241 |
/** |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
242 |
* Constructs a new throwable with {@code null} as its detail message. |
2 | 243 |
* The cause is not initialized, and may subsequently be initialized by a |
244 |
* call to {@link #initCause}. |
|
245 |
* |
|
246 |
* <p>The {@link #fillInStackTrace()} method is called to initialize |
|
247 |
* the stack trace data in the newly created throwable. |
|
248 |
*/ |
|
249 |
public Throwable() { |
|
250 |
fillInStackTrace(); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Constructs a new throwable with the specified detail message. The |
|
255 |
* cause is not initialized, and may subsequently be initialized by |
|
256 |
* a call to {@link #initCause}. |
|
257 |
* |
|
258 |
* <p>The {@link #fillInStackTrace()} method is called to initialize |
|
259 |
* the stack trace data in the newly created throwable. |
|
260 |
* |
|
261 |
* @param message the detail message. The detail message is saved for |
|
262 |
* later retrieval by the {@link #getMessage()} method. |
|
263 |
*/ |
|
264 |
public Throwable(String message) { |
|
265 |
fillInStackTrace(); |
|
266 |
detailMessage = message; |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Constructs a new throwable with the specified detail message and |
|
271 |
* cause. <p>Note that the detail message associated with |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
272 |
* {@code cause} is <i>not</i> automatically incorporated in |
2 | 273 |
* this throwable's detail message. |
274 |
* |
|
275 |
* <p>The {@link #fillInStackTrace()} method is called to initialize |
|
276 |
* the stack trace data in the newly created throwable. |
|
277 |
* |
|
278 |
* @param message the detail message (which is saved for later retrieval |
|
279 |
* by the {@link #getMessage()} method). |
|
280 |
* @param cause the cause (which is saved for later retrieval by the |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
281 |
* {@link #getCause()} method). (A {@code null} value is |
2 | 282 |
* permitted, and indicates that the cause is nonexistent or |
283 |
* unknown.) |
|
284 |
* @since 1.4 |
|
285 |
*/ |
|
286 |
public Throwable(String message, Throwable cause) { |
|
287 |
fillInStackTrace(); |
|
288 |
detailMessage = message; |
|
289 |
this.cause = cause; |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Constructs a new throwable with the specified cause and a detail |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
294 |
* message of {@code (cause==null ? null : cause.toString())} (which |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
295 |
* typically contains the class and detail message of {@code cause}). |
2 | 296 |
* This constructor is useful for throwables that are little more than |
297 |
* wrappers for other throwables (for example, {@link |
|
298 |
* java.security.PrivilegedActionException}). |
|
299 |
* |
|
300 |
* <p>The {@link #fillInStackTrace()} method is called to initialize |
|
301 |
* the stack trace data in the newly created throwable. |
|
302 |
* |
|
303 |
* @param cause the cause (which is saved for later retrieval by the |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
304 |
* {@link #getCause()} method). (A {@code null} value is |
2 | 305 |
* permitted, and indicates that the cause is nonexistent or |
306 |
* unknown.) |
|
307 |
* @since 1.4 |
|
308 |
*/ |
|
309 |
public Throwable(Throwable cause) { |
|
310 |
fillInStackTrace(); |
|
311 |
detailMessage = (cause==null ? null : cause.toString()); |
|
312 |
this.cause = cause; |
|
313 |
} |
|
314 |
||
315 |
/** |
|
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
316 |
* Constructs a new throwable with the specified detail message, |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
317 |
* cause, {@linkplain #addSuppressed suppression} enabled or |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
318 |
* disabled, and writable stack trace enabled or disabled. If |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
319 |
* suppression is disabled, {@link #getSuppressed} for this object |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
320 |
* will return a zero-length array and calls to {@link |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
321 |
* #addSuppressed} that would otherwise append an exception to the |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
322 |
* suppressed list will have no effect. If the writable stack |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
323 |
* trace is false, this constructor will not call {@link |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
324 |
* #fillInStackTrace()}, a {@code null} will be written to the |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
325 |
* {@code stackTrace} field, and subsequent calls to {@code |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
326 |
* fillInStackTrace} and {@link |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
327 |
* #setStackTrace(StackTraceElement[])} will not set the stack |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
328 |
* trace. If the writable stack trace is false, {@link |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
329 |
* #getStackTrace} will return a zero length array. |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
330 |
* |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
331 |
* <p>Note that the other constructors of {@code Throwable} treat |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
332 |
* suppression as being enabled and the stack trace as being |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
333 |
* writable. Subclasses of {@code Throwable} should document any |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
334 |
* conditions under which suppression is disabled and document |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
335 |
* conditions under which the stack trace is not writable. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
336 |
* Disabling of suppression should only occur in exceptional |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
337 |
* circumstances where special requirements exist, such as a |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
338 |
* virtual machine reusing exception objects under low-memory |
9683
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
339 |
* situations. Circumstances where a given exception object is |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
340 |
* repeatedly caught and rethrown, such as to implement control |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
341 |
* flow between two sub-systems, is another situation where |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
342 |
* immutable throwable objects would be appropriate. |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
343 |
* |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
344 |
* @param message the detail message. |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
345 |
* @param cause the cause. (A {@code null} value is permitted, |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
346 |
* and indicates that the cause is nonexistent or unknown.) |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
347 |
* @param enableSuppression whether or not suppression is enabled or disabled |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
348 |
* @param writableStackTrace whether or not the stack trace should be |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
349 |
* writable |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
350 |
* |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
351 |
* @see OutOfMemoryError |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
352 |
* @see NullPointerException |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
353 |
* @see ArithmeticException |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
354 |
* @since 1.7 |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
355 |
*/ |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
356 |
protected Throwable(String message, Throwable cause, |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
357 |
boolean enableSuppression, |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
358 |
boolean writableStackTrace) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
359 |
if (writableStackTrace) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
360 |
fillInStackTrace(); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
361 |
} else { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
362 |
stackTrace = null; |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
363 |
} |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
364 |
detailMessage = message; |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
365 |
this.cause = cause; |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
366 |
if (!enableSuppression) |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
367 |
suppressedExceptions = null; |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
368 |
} |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
369 |
|
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
370 |
/** |
2 | 371 |
* Returns the detail message string of this throwable. |
372 |
* |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
373 |
* @return the detail message string of this {@code Throwable} instance |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
374 |
* (which may be {@code null}). |
2 | 375 |
*/ |
376 |
public String getMessage() { |
|
377 |
return detailMessage; |
|
378 |
} |
|
379 |
||
380 |
/** |
|
381 |
* Creates a localized description of this throwable. |
|
382 |
* Subclasses may override this method in order to produce a |
|
383 |
* locale-specific message. For subclasses that do not override this |
|
384 |
* method, the default implementation returns the same result as |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
385 |
* {@code getMessage()}. |
2 | 386 |
* |
387 |
* @return The localized description of this throwable. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
22581
diff
changeset
|
388 |
* @since 1.1 |
2 | 389 |
*/ |
390 |
public String getLocalizedMessage() { |
|
391 |
return getMessage(); |
|
392 |
} |
|
393 |
||
394 |
/** |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
395 |
* Returns the cause of this throwable or {@code null} if the |
2 | 396 |
* cause is nonexistent or unknown. (The cause is the throwable that |
397 |
* caused this throwable to get thrown.) |
|
398 |
* |
|
399 |
* <p>This implementation returns the cause that was supplied via one of |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
400 |
* the constructors requiring a {@code Throwable}, or that was set after |
2 | 401 |
* creation with the {@link #initCause(Throwable)} method. While it is |
402 |
* typically unnecessary to override this method, a subclass can override |
|
403 |
* it to return a cause set by some other means. This is appropriate for |
|
404 |
* a "legacy chained throwable" that predates the addition of chained |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
405 |
* exceptions to {@code Throwable}. Note that it is <i>not</i> |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
406 |
* necessary to override any of the {@code PrintStackTrace} methods, |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
407 |
* all of which invoke the {@code getCause} method to determine the |
2 | 408 |
* cause of a throwable. |
409 |
* |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
410 |
* @return the cause of this throwable or {@code null} if the |
2 | 411 |
* cause is nonexistent or unknown. |
412 |
* @since 1.4 |
|
413 |
*/ |
|
6302 | 414 |
public synchronized Throwable getCause() { |
2 | 415 |
return (cause==this ? null : cause); |
416 |
} |
|
417 |
||
418 |
/** |
|
419 |
* Initializes the <i>cause</i> of this throwable to the specified value. |
|
420 |
* (The cause is the throwable that caused this throwable to get thrown.) |
|
421 |
* |
|
422 |
* <p>This method can be called at most once. It is generally called from |
|
423 |
* within the constructor, or immediately after creating the |
|
424 |
* throwable. If this throwable was created |
|
425 |
* with {@link #Throwable(Throwable)} or |
|
426 |
* {@link #Throwable(String,Throwable)}, this method cannot be called |
|
427 |
* even once. |
|
428 |
* |
|
9683
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
429 |
* <p>An example of using this method on a legacy throwable type |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
430 |
* without other support for setting the cause is: |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
431 |
* |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
432 |
* <pre> |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
433 |
* try { |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
434 |
* lowLevelOp(); |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
435 |
* } catch (LowLevelException le) { |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
436 |
* throw (HighLevelException) |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
437 |
* new HighLevelException().initCause(le); // Legacy constructor |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
438 |
* } |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
439 |
* </pre> |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
440 |
* |
2 | 441 |
* @param cause the cause (which is saved for later retrieval by the |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
442 |
* {@link #getCause()} method). (A {@code null} value is |
2 | 443 |
* permitted, and indicates that the cause is nonexistent or |
444 |
* unknown.) |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
445 |
* @return a reference to this {@code Throwable} instance. |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
446 |
* @throws IllegalArgumentException if {@code cause} is this |
2 | 447 |
* throwable. (A throwable cannot be its own cause.) |
448 |
* @throws IllegalStateException if this throwable was |
|
449 |
* created with {@link #Throwable(Throwable)} or |
|
450 |
* {@link #Throwable(String,Throwable)}, or this method has already |
|
451 |
* been called on this throwable. |
|
452 |
* @since 1.4 |
|
453 |
*/ |
|
454 |
public synchronized Throwable initCause(Throwable cause) { |
|
455 |
if (this.cause != this) |
|
17176
d7ee1e315fe7
8012044: Give more information about self-suppression from Throwable.addSuppressed
darcy
parents:
14342
diff
changeset
|
456 |
throw new IllegalStateException("Can't overwrite cause with " + |
d7ee1e315fe7
8012044: Give more information about self-suppression from Throwable.addSuppressed
darcy
parents:
14342
diff
changeset
|
457 |
Objects.toString(cause, "a null"), this); |
2 | 458 |
if (cause == this) |
17176
d7ee1e315fe7
8012044: Give more information about self-suppression from Throwable.addSuppressed
darcy
parents:
14342
diff
changeset
|
459 |
throw new IllegalArgumentException("Self-causation not permitted", this); |
2 | 460 |
this.cause = cause; |
461 |
return this; |
|
462 |
} |
|
463 |
||
464 |
/** |
|
465 |
* Returns a short description of this throwable. |
|
466 |
* The result is the concatenation of: |
|
467 |
* <ul> |
|
468 |
* <li> the {@linkplain Class#getName() name} of the class of this object |
|
469 |
* <li> ": " (a colon and a space) |
|
470 |
* <li> the result of invoking this object's {@link #getLocalizedMessage} |
|
471 |
* method |
|
472 |
* </ul> |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
473 |
* If {@code getLocalizedMessage} returns {@code null}, then just |
2 | 474 |
* the class name is returned. |
475 |
* |
|
476 |
* @return a string representation of this throwable. |
|
477 |
*/ |
|
478 |
public String toString() { |
|
479 |
String s = getClass().getName(); |
|
480 |
String message = getLocalizedMessage(); |
|
481 |
return (message != null) ? (s + ": " + message) : s; |
|
482 |
} |
|
483 |
||
484 |
/** |
|
485 |
* Prints this throwable and its backtrace to the |
|
486 |
* standard error stream. This method prints a stack trace for this |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
487 |
* {@code Throwable} object on the error output stream that is |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
488 |
* the value of the field {@code System.err}. The first line of |
2 | 489 |
* output contains the result of the {@link #toString()} method for |
490 |
* this object. Remaining lines represent data previously recorded by |
|
491 |
* the method {@link #fillInStackTrace()}. The format of this |
|
492 |
* information depends on the implementation, but the following |
|
493 |
* example may be regarded as typical: |
|
494 |
* <blockquote><pre> |
|
495 |
* java.lang.NullPointerException |
|
496 |
* at MyClass.mash(MyClass.java:9) |
|
497 |
* at MyClass.crunch(MyClass.java:6) |
|
498 |
* at MyClass.main(MyClass.java:3) |
|
499 |
* </pre></blockquote> |
|
500 |
* This example was produced by running the program: |
|
501 |
* <pre> |
|
502 |
* class MyClass { |
|
503 |
* public static void main(String[] args) { |
|
504 |
* crunch(null); |
|
505 |
* } |
|
506 |
* static void crunch(int[] a) { |
|
507 |
* mash(a); |
|
508 |
* } |
|
509 |
* static void mash(int[] b) { |
|
510 |
* System.out.println(b[0]); |
|
511 |
* } |
|
512 |
* } |
|
513 |
* </pre> |
|
514 |
* The backtrace for a throwable with an initialized, non-null cause |
|
515 |
* should generally include the backtrace for the cause. The format |
|
516 |
* of this information depends on the implementation, but the following |
|
517 |
* example may be regarded as typical: |
|
518 |
* <pre> |
|
519 |
* HighLevelException: MidLevelException: LowLevelException |
|
520 |
* at Junk.a(Junk.java:13) |
|
521 |
* at Junk.main(Junk.java:4) |
|
522 |
* Caused by: MidLevelException: LowLevelException |
|
523 |
* at Junk.c(Junk.java:23) |
|
524 |
* at Junk.b(Junk.java:17) |
|
525 |
* at Junk.a(Junk.java:11) |
|
526 |
* ... 1 more |
|
527 |
* Caused by: LowLevelException |
|
528 |
* at Junk.e(Junk.java:30) |
|
529 |
* at Junk.d(Junk.java:27) |
|
530 |
* at Junk.c(Junk.java:21) |
|
531 |
* ... 3 more |
|
532 |
* </pre> |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
533 |
* Note the presence of lines containing the characters {@code "..."}. |
2 | 534 |
* These lines indicate that the remainder of the stack trace for this |
535 |
* exception matches the indicated number of frames from the bottom of the |
|
536 |
* stack trace of the exception that was caused by this exception (the |
|
537 |
* "enclosing" exception). This shorthand can greatly reduce the length |
|
538 |
* of the output in the common case where a wrapped exception is thrown |
|
539 |
* from same method as the "causative exception" is caught. The above |
|
540 |
* example was produced by running the program: |
|
541 |
* <pre> |
|
542 |
* public class Junk { |
|
543 |
* public static void main(String args[]) { |
|
544 |
* try { |
|
545 |
* a(); |
|
546 |
* } catch(HighLevelException e) { |
|
547 |
* e.printStackTrace(); |
|
548 |
* } |
|
549 |
* } |
|
550 |
* static void a() throws HighLevelException { |
|
551 |
* try { |
|
552 |
* b(); |
|
553 |
* } catch(MidLevelException e) { |
|
554 |
* throw new HighLevelException(e); |
|
555 |
* } |
|
556 |
* } |
|
557 |
* static void b() throws MidLevelException { |
|
558 |
* c(); |
|
559 |
* } |
|
560 |
* static void c() throws MidLevelException { |
|
561 |
* try { |
|
562 |
* d(); |
|
563 |
* } catch(LowLevelException e) { |
|
564 |
* throw new MidLevelException(e); |
|
565 |
* } |
|
566 |
* } |
|
567 |
* static void d() throws LowLevelException { |
|
568 |
* e(); |
|
569 |
* } |
|
570 |
* static void e() throws LowLevelException { |
|
571 |
* throw new LowLevelException(); |
|
572 |
* } |
|
573 |
* } |
|
574 |
* |
|
575 |
* class HighLevelException extends Exception { |
|
576 |
* HighLevelException(Throwable cause) { super(cause); } |
|
577 |
* } |
|
578 |
* |
|
579 |
* class MidLevelException extends Exception { |
|
580 |
* MidLevelException(Throwable cause) { super(cause); } |
|
581 |
* } |
|
582 |
* |
|
583 |
* class LowLevelException extends Exception { |
|
584 |
* } |
|
585 |
* </pre> |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
586 |
* As of release 7, the platform supports the notion of |
6515
7a5ccc90b436
6980019: Finish rename of ARM -> try-with-resources in jdk repository
darcy
parents:
6302
diff
changeset
|
587 |
* <i>suppressed exceptions</i> (in conjunction with the {@code |
7a5ccc90b436
6980019: Finish rename of ARM -> try-with-resources in jdk repository
darcy
parents:
6302
diff
changeset
|
588 |
* try}-with-resources statement). Any exceptions that were |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
589 |
* suppressed in order to deliver an exception are printed out |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
590 |
* beneath the stack trace. The format of this information |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
591 |
* depends on the implementation, but the following example may be |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
592 |
* regarded as typical: |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
593 |
* |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
594 |
* <pre> |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
595 |
* Exception in thread "main" java.lang.Exception: Something happened |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
596 |
* at Foo.bar(Foo.java:10) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
597 |
* at Foo.main(Foo.java:5) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
598 |
* Suppressed: Resource$CloseFailException: Resource ID = 0 |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
599 |
* at Resource.close(Resource.java:26) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
600 |
* at Foo.bar(Foo.java:9) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
601 |
* ... 1 more |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
602 |
* </pre> |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
603 |
* Note that the "... n more" notation is used on suppressed exceptions |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
604 |
* just at it is used on causes. Unlike causes, suppressed exceptions are |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
605 |
* indented beyond their "containing exceptions." |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
606 |
* |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
607 |
* <p>An exception can have both a cause and one or more suppressed |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
608 |
* exceptions: |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
609 |
* <pre> |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
610 |
* Exception in thread "main" java.lang.Exception: Main block |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
611 |
* at Foo3.main(Foo3.java:7) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
612 |
* Suppressed: Resource$CloseFailException: Resource ID = 2 |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
613 |
* at Resource.close(Resource.java:26) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
614 |
* at Foo3.main(Foo3.java:5) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
615 |
* Suppressed: Resource$CloseFailException: Resource ID = 1 |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
616 |
* at Resource.close(Resource.java:26) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
617 |
* at Foo3.main(Foo3.java:5) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
618 |
* Caused by: java.lang.Exception: I did it |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
619 |
* at Foo3.main(Foo3.java:8) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
620 |
* </pre> |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
621 |
* Likewise, a suppressed exception can have a cause: |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
622 |
* <pre> |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
623 |
* Exception in thread "main" java.lang.Exception: Main block |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
624 |
* at Foo4.main(Foo4.java:6) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
625 |
* Suppressed: Resource2$CloseFailException: Resource ID = 1 |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
626 |
* at Resource2.close(Resource2.java:20) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
627 |
* at Foo4.main(Foo4.java:5) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
628 |
* Caused by: java.lang.Exception: Rats, you caught me |
11517
94d10ec9083a
7128931: Bad HTML escaping in java.lang.Throwable javadoc
darcy
parents:
9703
diff
changeset
|
629 |
* at Resource2$CloseFailException.<init>(Resource2.java:45) |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
630 |
* ... 2 more |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
631 |
* </pre> |
2 | 632 |
*/ |
633 |
public void printStackTrace() { |
|
634 |
printStackTrace(System.err); |
|
635 |
} |
|
636 |
||
637 |
/** |
|
638 |
* Prints this throwable and its backtrace to the specified print stream. |
|
639 |
* |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
640 |
* @param s {@code PrintStream} to use for output |
2 | 641 |
*/ |
642 |
public void printStackTrace(PrintStream s) { |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
643 |
printStackTrace(new WrappedPrintStream(s)); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
644 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
645 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
646 |
private void printStackTrace(PrintStreamOrWriter s) { |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
647 |
// Guard against malicious overrides of Throwable.equals by |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
648 |
// using a Set with identity equality semantics. |
22581
e868cde95050
8032779: Update code in java.lang to use newer language features
psandoz
parents:
17176
diff
changeset
|
649 |
Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>()); |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
650 |
dejaVu.add(this); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
651 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
652 |
synchronized (s.lock()) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
653 |
// Print our stack trace |
2 | 654 |
s.println(this); |
655 |
StackTraceElement[] trace = getOurStackTrace(); |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
656 |
for (StackTraceElement traceElement : trace) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
657 |
s.println("\tat " + traceElement); |
2 | 658 |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
659 |
// Print suppressed exceptions, if any |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
660 |
for (Throwable se : getSuppressed()) |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
661 |
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
662 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
663 |
// Print cause, if any |
2 | 664 |
Throwable ourCause = getCause(); |
665 |
if (ourCause != null) |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
666 |
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu); |
2 | 667 |
} |
668 |
} |
|
669 |
||
670 |
/** |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
671 |
* Print our stack trace as an enclosed exception for the specified |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
672 |
* stack trace. |
2 | 673 |
*/ |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
674 |
private void printEnclosedStackTrace(PrintStreamOrWriter s, |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
675 |
StackTraceElement[] enclosingTrace, |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
676 |
String caption, |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
677 |
String prefix, |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
678 |
Set<Throwable> dejaVu) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
679 |
assert Thread.holdsLock(s.lock()); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
680 |
if (dejaVu.contains(this)) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
681 |
s.println("\t[CIRCULAR REFERENCE:" + this + "]"); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
682 |
} else { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
683 |
dejaVu.add(this); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
684 |
// Compute number of frames in common between this and enclosing trace |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
685 |
StackTraceElement[] trace = getOurStackTrace(); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
686 |
int m = trace.length - 1; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
687 |
int n = enclosingTrace.length - 1; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
688 |
while (m >= 0 && n >=0 && trace[m].equals(enclosingTrace[n])) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
689 |
m--; n--; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
690 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
691 |
int framesInCommon = trace.length - 1 - m; |
2 | 692 |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
693 |
// Print our stack trace |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
694 |
s.println(prefix + caption + this); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
695 |
for (int i = 0; i <= m; i++) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
696 |
s.println(prefix + "\tat " + trace[i]); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
697 |
if (framesInCommon != 0) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
698 |
s.println(prefix + "\t... " + framesInCommon + " more"); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
699 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
700 |
// Print suppressed exceptions, if any |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
701 |
for (Throwable se : getSuppressed()) |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
702 |
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
703 |
prefix +"\t", dejaVu); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
704 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
705 |
// Print cause, if any |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
706 |
Throwable ourCause = getCause(); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
707 |
if (ourCause != null) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
708 |
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu); |
2 | 709 |
} |
710 |
} |
|
711 |
||
712 |
/** |
|
713 |
* Prints this throwable and its backtrace to the specified |
|
714 |
* print writer. |
|
715 |
* |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
716 |
* @param s {@code PrintWriter} to use for output |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
22581
diff
changeset
|
717 |
* @since 1.1 |
2 | 718 |
*/ |
719 |
public void printStackTrace(PrintWriter s) { |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
720 |
printStackTrace(new WrappedPrintWriter(s)); |
2 | 721 |
} |
722 |
||
723 |
/** |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
724 |
* Wrapper class for PrintStream and PrintWriter to enable a single |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
725 |
* implementation of printStackTrace. |
2 | 726 |
*/ |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
727 |
private abstract static class PrintStreamOrWriter { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
728 |
/** Returns the object to be locked when using this StreamOrWriter */ |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
729 |
abstract Object lock(); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
730 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
731 |
/** Prints the specified string as a line on this StreamOrWriter */ |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
732 |
abstract void println(Object o); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
733 |
} |
2 | 734 |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
735 |
private static class WrappedPrintStream extends PrintStreamOrWriter { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
736 |
private final PrintStream printStream; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
737 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
738 |
WrappedPrintStream(PrintStream printStream) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
739 |
this.printStream = printStream; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
740 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
741 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
742 |
Object lock() { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
743 |
return printStream; |
2 | 744 |
} |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
745 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
746 |
void println(Object o) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
747 |
printStream.println(o); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
748 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
749 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
750 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
751 |
private static class WrappedPrintWriter extends PrintStreamOrWriter { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
752 |
private final PrintWriter printWriter; |
2 | 753 |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
754 |
WrappedPrintWriter(PrintWriter printWriter) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
755 |
this.printWriter = printWriter; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
756 |
} |
2 | 757 |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
758 |
Object lock() { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
759 |
return printWriter; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
760 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
761 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
762 |
void println(Object o) { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
763 |
printWriter.println(o); |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
764 |
} |
2 | 765 |
} |
766 |
||
767 |
/** |
|
768 |
* Fills in the execution stack trace. This method records within this |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
769 |
* {@code Throwable} object information about the current state of |
2 | 770 |
* the stack frames for the current thread. |
771 |
* |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
772 |
* <p>If the stack trace of this {@code Throwable} {@linkplain |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
773 |
* Throwable#Throwable(String, Throwable, boolean, boolean) is not |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
774 |
* writable}, calling this method has no effect. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
775 |
* |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
776 |
* @return a reference to this {@code Throwable} instance. |
2 | 777 |
* @see java.lang.Throwable#printStackTrace() |
778 |
*/ |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
779 |
public synchronized Throwable fillInStackTrace() { |
9703
f607d3a94d70
7045138: OutOfMemoryError thrown without stack trace in jdk7-b142
darcy
parents:
9683
diff
changeset
|
780 |
if (stackTrace != null || |
f607d3a94d70
7045138: OutOfMemoryError thrown without stack trace in jdk7-b142
darcy
parents:
9683
diff
changeset
|
781 |
backtrace != null /* Out of protocol state */ ) { |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
782 |
fillInStackTrace(0); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
783 |
stackTrace = UNASSIGNED_STACK; |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
784 |
} |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
785 |
return this; |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
786 |
} |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
787 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
788 |
private native Throwable fillInStackTrace(int dummy); |
2 | 789 |
|
790 |
/** |
|
791 |
* Provides programmatic access to the stack trace information printed by |
|
792 |
* {@link #printStackTrace()}. Returns an array of stack trace elements, |
|
793 |
* each representing one stack frame. The zeroth element of the array |
|
794 |
* (assuming the array's length is non-zero) represents the top of the |
|
795 |
* stack, which is the last method invocation in the sequence. Typically, |
|
796 |
* this is the point at which this throwable was created and thrown. |
|
797 |
* The last element of the array (assuming the array's length is non-zero) |
|
798 |
* represents the bottom of the stack, which is the first method invocation |
|
799 |
* in the sequence. |
|
800 |
* |
|
801 |
* <p>Some virtual machines may, under some circumstances, omit one |
|
802 |
* or more stack frames from the stack trace. In the extreme case, |
|
803 |
* a virtual machine that has no stack trace information concerning |
|
804 |
* this throwable is permitted to return a zero-length array from this |
|
805 |
* method. Generally speaking, the array returned by this method will |
|
806 |
* contain one element for every frame that would be printed by |
|
9683
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
807 |
* {@code printStackTrace}. Writes to the returned array do not |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
808 |
* affect future calls to this method. |
2 | 809 |
* |
810 |
* @return an array of stack trace elements representing the stack trace |
|
811 |
* pertaining to this throwable. |
|
812 |
* @since 1.4 |
|
813 |
*/ |
|
814 |
public StackTraceElement[] getStackTrace() { |
|
815 |
return getOurStackTrace().clone(); |
|
816 |
} |
|
817 |
||
818 |
private synchronized StackTraceElement[] getOurStackTrace() { |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
819 |
// Initialize stack trace field with information from |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
820 |
// backtrace if this is the first call to this method |
9703
f607d3a94d70
7045138: OutOfMemoryError thrown without stack trace in jdk7-b142
darcy
parents:
9683
diff
changeset
|
821 |
if (stackTrace == UNASSIGNED_STACK || |
f607d3a94d70
7045138: OutOfMemoryError thrown without stack trace in jdk7-b142
darcy
parents:
9683
diff
changeset
|
822 |
(stackTrace == null && backtrace != null) /* Out of protocol state */) { |
2 | 823 |
int depth = getStackTraceDepth(); |
824 |
stackTrace = new StackTraceElement[depth]; |
|
825 |
for (int i=0; i < depth; i++) |
|
826 |
stackTrace[i] = getStackTraceElement(i); |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
827 |
} else if (stackTrace == null) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
828 |
return UNASSIGNED_STACK; |
2 | 829 |
} |
830 |
return stackTrace; |
|
831 |
} |
|
832 |
||
833 |
/** |
|
834 |
* Sets the stack trace elements that will be returned by |
|
835 |
* {@link #getStackTrace()} and printed by {@link #printStackTrace()} |
|
836 |
* and related methods. |
|
837 |
* |
|
838 |
* This method, which is designed for use by RPC frameworks and other |
|
839 |
* advanced systems, allows the client to override the default |
|
840 |
* stack trace that is either generated by {@link #fillInStackTrace()} |
|
841 |
* when a throwable is constructed or deserialized when a throwable is |
|
842 |
* read from a serialization stream. |
|
843 |
* |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
844 |
* <p>If the stack trace of this {@code Throwable} {@linkplain |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
845 |
* Throwable#Throwable(String, Throwable, boolean, boolean) is not |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
846 |
* writable}, calling this method has no effect other than |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
847 |
* validating its argument. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
848 |
* |
2 | 849 |
* @param stackTrace the stack trace elements to be associated with |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
850 |
* this {@code Throwable}. The specified array is copied by this |
2 | 851 |
* call; changes in the specified array after the method invocation |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
852 |
* returns will have no affect on this {@code Throwable}'s stack |
2 | 853 |
* trace. |
854 |
* |
|
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
855 |
* @throws NullPointerException if {@code stackTrace} is |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
856 |
* {@code null} or if any of the elements of |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
857 |
* {@code stackTrace} are {@code null} |
2 | 858 |
* |
859 |
* @since 1.4 |
|
860 |
*/ |
|
861 |
public void setStackTrace(StackTraceElement[] stackTrace) { |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
862 |
// Validate argument |
2 | 863 |
StackTraceElement[] defensiveCopy = stackTrace.clone(); |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
864 |
for (int i = 0; i < defensiveCopy.length; i++) { |
2 | 865 |
if (defensiveCopy[i] == null) |
866 |
throw new NullPointerException("stackTrace[" + i + "]"); |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
867 |
} |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
868 |
|
6302 | 869 |
synchronized (this) { |
9703
f607d3a94d70
7045138: OutOfMemoryError thrown without stack trace in jdk7-b142
darcy
parents:
9683
diff
changeset
|
870 |
if (this.stackTrace == null && // Immutable stack |
f607d3a94d70
7045138: OutOfMemoryError thrown without stack trace in jdk7-b142
darcy
parents:
9683
diff
changeset
|
871 |
backtrace == null) // Test for out of protocol state |
9534
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
872 |
return; |
6302 | 873 |
this.stackTrace = defensiveCopy; |
874 |
} |
|
2 | 875 |
} |
876 |
||
877 |
/** |
|
878 |
* Returns the number of elements in the stack trace (or 0 if the stack |
|
879 |
* trace is unavailable). |
|
2947
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2
diff
changeset
|
880 |
* |
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2
diff
changeset
|
881 |
* package-protection for use by SharedSecrets. |
2 | 882 |
*/ |
2947
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2
diff
changeset
|
883 |
native int getStackTraceDepth(); |
2 | 884 |
|
885 |
/** |
|
886 |
* Returns the specified element of the stack trace. |
|
887 |
* |
|
2947
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2
diff
changeset
|
888 |
* package-protection for use by SharedSecrets. |
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2
diff
changeset
|
889 |
* |
2 | 890 |
* @param index index of the element to return. |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
891 |
* @throws IndexOutOfBoundsException if {@code index < 0 || |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
892 |
* index >= getStackTraceDepth() } |
2 | 893 |
*/ |
2947
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2
diff
changeset
|
894 |
native StackTraceElement getStackTraceElement(int index); |
2 | 895 |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
896 |
/** |
7988
d31b7cc371ef
7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents:
7803
diff
changeset
|
897 |
* Reads a {@code Throwable} from a stream, enforcing |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
898 |
* well-formedness constraints on fields. Null entries and |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
899 |
* self-pointers are not allowed in the list of {@code |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
900 |
* suppressedExceptions}. Null entries are not allowed for stack |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
901 |
* trace elements. A null stack trace in the serial form results |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
902 |
* in a zero-length stack element array. A single-element stack |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
903 |
* trace whose entry is equal to {@code new StackTraceElement("", |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
904 |
* "", null, Integer.MIN_VALUE)} results in a {@code null} {@code |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
905 |
* stackTrace} field. |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
906 |
* |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
907 |
* Note that there are no constraints on the value the {@code |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
908 |
* cause} field can hold; both {@code null} and {@code this} are |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
909 |
* valid values for the field. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
910 |
*/ |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
911 |
private void readObject(ObjectInputStream s) |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
912 |
throws IOException, ClassNotFoundException { |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
913 |
s.defaultReadObject(); // read in all fields |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
914 |
if (suppressedExceptions != null) { |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
915 |
List<Throwable> suppressed = null; |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
916 |
if (suppressedExceptions.isEmpty()) { |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
917 |
// Use the sentinel for a zero-length list |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
918 |
suppressed = SUPPRESSED_SENTINEL; |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
919 |
} else { // Copy Throwables to new list |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7186
diff
changeset
|
920 |
suppressed = new ArrayList<>(1); |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
921 |
for (Throwable t : suppressedExceptions) { |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
922 |
// Enforce constraints on suppressed exceptions in |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
923 |
// case of corrupt or malicious stream. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
924 |
if (t == null) |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
925 |
throw new NullPointerException(NULL_CAUSE_MESSAGE); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
926 |
if (t == this) |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
927 |
throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
928 |
suppressed.add(t); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
929 |
} |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
930 |
} |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
931 |
suppressedExceptions = suppressed; |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
932 |
} // else a null suppressedExceptions field remains null |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
933 |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
934 |
/* |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
935 |
* For zero-length stack traces, use a clone of |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
936 |
* UNASSIGNED_STACK rather than UNASSIGNED_STACK itself to |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
937 |
* allow identity comparison against UNASSIGNED_STACK in |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
938 |
* getOurStackTrace. The identity of UNASSIGNED_STACK in |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
939 |
* stackTrace indicates to the getOurStackTrace method that |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
940 |
* the stackTrace needs to be constructed from the information |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
941 |
* in backtrace. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
942 |
*/ |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
943 |
if (stackTrace != null) { |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
944 |
if (stackTrace.length == 0) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
945 |
stackTrace = UNASSIGNED_STACK.clone(); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
946 |
} else if (stackTrace.length == 1 && |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
947 |
// Check for the marker of an immutable stack trace |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
948 |
SentinelHolder.STACK_TRACE_ELEMENT_SENTINEL.equals(stackTrace[0])) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
949 |
stackTrace = null; |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
950 |
} else { // Verify stack trace elements are non-null. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
951 |
for(StackTraceElement ste : stackTrace) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
952 |
if (ste == null) |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
953 |
throw new NullPointerException("null StackTraceElement in serial stream. "); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
954 |
} |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
955 |
} |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
956 |
} else { |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
957 |
// A null stackTrace field in the serial form can result |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
958 |
// from an exception serialized without that field in |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
959 |
// older JDK releases; treat such exceptions as having |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
960 |
// empty stack traces. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
961 |
stackTrace = UNASSIGNED_STACK.clone(); |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
962 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
963 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
964 |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
965 |
/** |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
966 |
* Write a {@code Throwable} object to a stream. |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
967 |
* |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
968 |
* A {@code null} stack trace field is represented in the serial |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
969 |
* form as a one-element array whose element is equal to {@code |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
970 |
* new StackTraceElement("", "", null, Integer.MIN_VALUE)}. |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
971 |
*/ |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
972 |
private synchronized void writeObject(ObjectOutputStream s) |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
973 |
throws IOException { |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
974 |
// Ensure that the stackTrace field is initialized to a |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
975 |
// non-null value, if appropriate. As of JDK 7, a null stack |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
976 |
// trace field is a valid value indicating the stack trace |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
977 |
// should not be set. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
978 |
getOurStackTrace(); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
979 |
|
9534
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
980 |
StackTraceElement[] oldStackTrace = stackTrace; |
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
981 |
try { |
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
982 |
if (stackTrace == null) |
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
983 |
stackTrace = SentinelHolder.STACK_TRACE_SENTINEL; |
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
984 |
s.defaultWriteObject(); |
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
985 |
} finally { |
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
986 |
stackTrace = oldStackTrace; |
080badb816ac
7038843: IIOP serialization fails with NullPointerException when serializing Throwable
darcy
parents:
9513
diff
changeset
|
987 |
} |
2 | 988 |
} |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
989 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
990 |
/** |
7988
d31b7cc371ef
7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents:
7803
diff
changeset
|
991 |
* Appends the specified exception to the exceptions that were |
d31b7cc371ef
7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents:
7803
diff
changeset
|
992 |
* suppressed in order to deliver this exception. This method is |
9683
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
993 |
* thread-safe and typically called (automatically and implicitly) |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
994 |
* by the {@code try}-with-resources statement. |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
995 |
* |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
996 |
* <p>The suppression behavior is enabled <em>unless</em> disabled |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
997 |
* {@linkplain #Throwable(String, Throwable, boolean, boolean) via |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
998 |
* a constructor}. When suppression is disabled, this method does |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
999 |
* nothing other than to validate its argument. |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1000 |
* |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
1001 |
* <p>Note that when one exception {@linkplain |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
1002 |
* #initCause(Throwable) causes} another exception, the first |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
1003 |
* exception is usually caught and then the second exception is |
9005
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1004 |
* thrown in response. In other words, there is a causal |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1005 |
* connection between the two exceptions. |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1006 |
* |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1007 |
* In contrast, there are situations where two independent |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1008 |
* exceptions can be thrown in sibling code blocks, in particular |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1009 |
* in the {@code try} block of a {@code try}-with-resources |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1010 |
* statement and the compiler-generated {@code finally} block |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1011 |
* which closes the resource. |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1012 |
* |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1013 |
* In these situations, only one of the thrown exceptions can be |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1014 |
* propagated. In the {@code try}-with-resources statement, when |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1015 |
* there are two such exceptions, the exception originating from |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1016 |
* the {@code try} block is propagated and the exception from the |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1017 |
* {@code finally} block is added to the list of exceptions |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1018 |
* suppressed by the exception from the {@code try} block. As an |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1019 |
* exception unwinds the stack, it can accumulate multiple |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1020 |
* suppressed exceptions. |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1021 |
* |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1022 |
* <p>An exception may have suppressed exceptions while also being |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1023 |
* caused by another exception. Whether or not an exception has a |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1024 |
* cause is semantically known at the time of its creation, unlike |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1025 |
* whether or not an exception will suppress other exceptions |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1026 |
* which is typically only determined after an exception is |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1027 |
* thrown. |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1028 |
* |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1029 |
* <p>Note that programmer written code is also able to take |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1030 |
* advantage of calling this method in situations where there are |
a329d2b300c9
7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents:
7988
diff
changeset
|
1031 |
* multiple sibling exceptions and only one can be propagated. |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
1032 |
* |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1033 |
* @param exception the exception to be added to the list of |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1034 |
* suppressed exceptions |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
1035 |
* @throws IllegalArgumentException if {@code exception} is this |
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
1036 |
* throwable; a throwable cannot suppress itself. |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1037 |
* @throws NullPointerException if {@code exception} is {@code null} |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1038 |
* @since 1.7 |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1039 |
*/ |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1040 |
public final synchronized void addSuppressed(Throwable exception) { |
6110
6db4b0180328
6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents:
5972
diff
changeset
|
1041 |
if (exception == this) |
17176
d7ee1e315fe7
8012044: Give more information about self-suppression from Throwable.addSuppressed
darcy
parents:
14342
diff
changeset
|
1042 |
throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception); |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1043 |
|
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1044 |
if (exception == null) |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1045 |
throw new NullPointerException(NULL_CAUSE_MESSAGE); |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1046 |
|
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1047 |
if (suppressedExceptions == null) // Suppressed exceptions not recorded |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1048 |
return; |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1049 |
|
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1050 |
if (suppressedExceptions == SUPPRESSED_SENTINEL) |
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1051 |
suppressedExceptions = new ArrayList<>(1); |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1052 |
|
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1053 |
suppressedExceptions.add(exception); |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1054 |
} |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1055 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1056 |
private static final Throwable[] EMPTY_THROWABLE_ARRAY = new Throwable[0]; |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1057 |
|
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1058 |
/** |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1059 |
* Returns an array containing all of the exceptions that were |
6515
7a5ccc90b436
6980019: Finish rename of ARM -> try-with-resources in jdk repository
darcy
parents:
6302
diff
changeset
|
1060 |
* suppressed, typically by the {@code try}-with-resources |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1061 |
* statement, in order to deliver this exception. |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1062 |
* |
9020
13b639abc930
7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents:
9005
diff
changeset
|
1063 |
* If no exceptions were suppressed or {@linkplain |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
9266
diff
changeset
|
1064 |
* #Throwable(String, Throwable, boolean, boolean) suppression is |
9683
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
1065 |
* disabled}, an empty array is returned. This method is |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
1066 |
* thread-safe. Writes to the returned array do not affect future |
75c24fb3b3d1
7021645: Project Coin: Minor improvements to java.lang.Throwable
darcy
parents:
9534
diff
changeset
|
1067 |
* calls to this method. |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1068 |
* |
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1069 |
* @return an array containing all of the exceptions that were |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1070 |
* suppressed to deliver this exception. |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1071 |
* @since 1.7 |
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1072 |
*/ |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1073 |
public final synchronized Throwable[] getSuppressed() { |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1074 |
if (suppressedExceptions == SUPPRESSED_SENTINEL || |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
6515
diff
changeset
|
1075 |
suppressedExceptions == null) |
6302 | 1076 |
return EMPTY_THROWABLE_ARRAY; |
1077 |
else |
|
1078 |
return suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY); |
|
5972
e3f47656e9d9
6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
5506
diff
changeset
|
1079 |
} |
2 | 1080 |
} |