jdk/src/share/classes/java/lang/Throwable.java
author jjh
Wed, 13 Apr 2011 12:16:13 -0700
changeset 9266 121fb370f179
parent 9020 13b639abc930
child 9513 1079ae7ada52
permissions -rw-r--r--
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks 7032965: API files in java.io need to updated for references to JVM Spec with editions/hyperlinks 7032958: API files in java.lang need to updated for references to JLS with editions/hyperlinks 7032961: API files in java.lang need to updated for references to JVM with editions/hyperlinks 7032976: API files in javax.lang need to be updated for references to JLS with editions/hyperlinks 7032959: API files in java.util need to updated for references to JLS with editions/hyperlinks 7032962: API files in java.util need to updated for references to JVM Spec with editions/hyperlinks 7032967: API files in java.security need to updated for references to JVM Spec with editions/hyperlinks 7032955: API files in java.math need to updated for references to JLS with editions/hyperlinks Summary: Removed URLs and 'edition' references Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 7803
diff changeset
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4666
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4666
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4666
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4666
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4666
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * exceptions in the Java language. Only objects that are instances of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>Instances of two subclasses, {@link java.lang.Error} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@link java.lang.Exception}, are conventionally used to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * that exceptional situations have occurred. Typically, these instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * are freshly created in the context of the exceptional situation so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * as to include relevant information (such as stack trace data).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>One reason that a throwable may have a cause is that the class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * throws it is built atop a lower layered abstraction, and an operation on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the upper layer fails due to a failure in the lower layer.  It would be bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * design to let the throwable thrown by the lower layer propagate outward, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * it is generally unrelated to the abstraction provided by the upper layer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Further, doing so would tie the API of the upper layer to the details of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * its implementation, assuming the lower layer's exception was a checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * exception.  Throwing a "wrapped exception" (i.e., an exception containing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * cause) allows the upper layer to communicate the details of the failure to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * its caller without incurring either of these shortcomings.  It preserves
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the flexibility to change the implementation of the upper layer without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * changing its API (in particular, the set of exceptions thrown by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * methods).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p>A second reason that a throwable may have a cause is that the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * that throws it must conform to a general-purpose interface that does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * permit the method to throw the cause directly.  For example, suppose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * a persistent collection conforms to the {@link java.util.Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * specification for the persistent collection should indicate that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * capable of throwing such exceptions.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>A cause can be associated with a throwable in two ways: via a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * constructor that takes the cause as an argument, or via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * {@link #initCause(Throwable)} method.  New throwable classes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * wish to allow causes to be associated with them should provide constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * associated with any throwable, even a "legacy throwable" whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Further, those subclasses that might likely have a cause associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * cause).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @author  Josh Bloch (Added exception chaining and programmatic access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
public class Throwable implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static final long serialVersionUID = -3042686055658047285L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Native code saves some indication of the stack backtrace in this slot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private transient Object backtrace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * the file that could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private String detailMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   133
     * A shared value for an empty stack.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   134
     */
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   135
    private static final StackTraceElement[] EMPTY_STACK = new StackTraceElement[0];
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   136
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   137
    /*
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   138
     * To allow Throwable objects to be made immutable and safely
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   139
     * reused by the JVM, such as OutOfMemoryErrors, fields of
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   140
     * Throwable that are writable in response to user actions, cause
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   141
     * and suppressedExceptions obey the following protocol:
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   142
     *
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   143
     * 1) The fields are initialized to a non-null sentinel value
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   144
     * which indicates the value has logically not been set.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   145
     *
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   146
     * 2) Writing a null to the field indicates further writes
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   147
     * are forbidden
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   148
     *
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   149
     * 3) The sentinel value may be replaced with another non-null
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   150
     * value.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   151
     *
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   152
     * For example, implementations of the HotSpot JVM have
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   153
     * preallocated OutOfMemoryError objects to provide for better
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   154
     * diagnosability of that situation.  These objects are created
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   155
     * without calling the constructor for that class and the fields
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   156
     * in question are initialized to null.  To support this
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   157
     * capability, any new fields added to Throwable that require
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   158
     * being initialized to a non-null value require a coordinated JVM
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   159
     * change.
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
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * The throwable that caused this throwable to get thrown, or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * throwable was not caused by another throwable, or if the causative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * throwable is unknown.  If this field is equal to this throwable itself,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * it indicates that the cause of this throwable has not yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private Throwable cause = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * The stack trace, as returned by {@link #getStackTrace()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private StackTraceElement[] stackTrace;
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   181
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   182
    // Setting this static field introduces an acceptable
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   183
    // initialization dependency on a few java.util classes.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   184
    private static final List<Throwable> SUPPRESSED_SENTINEL =
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   185
        Collections.unmodifiableList(new ArrayList<Throwable>(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   188
     * The list of suppressed exceptions, as returned by {@link
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   189
     * #getSuppressed()}.  The list is initialized to a zero-element
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   190
     * unmodifiable sentinel list.  When a serialized Throwable is
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   191
     * read in, if the {@code suppressedExceptions} field points to a
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   192
     * 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
   193
     *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   194
     * @serial
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   195
     * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   196
     */
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   197
    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
   198
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   199
    /** 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
   200
    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
   201
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   202
    /** Message for trying to suppress oneself. */
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   203
    private static final String SELF_SUPPRESSION_MESSAGE = "Self-suppression not permitted";
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   204
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   205
    /** 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
   206
    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
   207
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   208
    /** 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
   209
    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
   210
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   211
    /**
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   212
     * Constructs a new throwable with {@code null} as its detail message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * The cause is not initialized, and may subsequently be initialized by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * call to {@link #initCause}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public Throwable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Constructs a new throwable with the specified detail message.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * cause is not initialized, and may subsequently be initialized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * a call to {@link #initCause}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param   message   the detail message. The detail message is saved for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          later retrieval by the {@link #getMessage()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public Throwable(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        detailMessage = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Constructs a new throwable with the specified detail message and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * cause.  <p>Note that the detail message associated with
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   242
     * {@code cause} is <i>not</i> automatically incorporated in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * this throwable's detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param  message the detail message (which is saved for later retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *         by the {@link #getMessage()} method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @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
   251
     *         {@link #getCause()} method).  (A {@code null} value is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         permitted, and indicates that the cause is nonexistent or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *         unknown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public Throwable(String message, Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        detailMessage = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * 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
   264
     * message of {@code (cause==null ? null : cause.toString())} (which
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   265
     * typically contains the class and detail message of {@code cause}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * This constructor is useful for throwables that are little more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * wrappers for other throwables (for example, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * java.security.PrivilegedActionException}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @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
   274
     *         {@link #getCause()} method).  (A {@code null} value is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *         permitted, and indicates that the cause is nonexistent or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *         unknown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public Throwable(Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        detailMessage = (cause==null ? null : cause.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   286
     * Constructs a new throwable with the specified detail message,
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   287
     * cause, and {@linkplain #addSuppressed suppression} enabled or
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   288
     * disabled.  If suppression is disabled, {@link #getSuppressed}
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   289
     * for this object will return a zero-length array and calls to
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   290
     * {@link #addSuppressed} that would otherwise append an exception
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   291
     * to the suppressed list will have no effect.
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   292
     *
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   293
     * <p>Note that the other constructors of {@code Throwable} treat
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   294
     * suppression as being enabled.  Subclasses of {@code Throwable}
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   295
     * should document any conditions under which suppression is
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   296
     * disabled.  Disabling of suppression should only occur in
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   297
     * exceptional circumstances where special requirements exist,
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   298
     * such as a virtual machine reusing exception objects under
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   299
     * low-memory situations.
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   300
     *
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   301
     * @param  message the detail message.
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   302
     * @param cause the cause.  (A {@code null} value is permitted,
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   303
     * and indicates that the cause is nonexistent or unknown.)
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   304
     * @param enableSuppression whether or not suppression is enabled or disabled
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   305
     *
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   306
     * @see OutOfMemoryError
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   307
     * @see NullPointerException
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   308
     * @see ArithmeticException
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   309
     * @since 1.7
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   310
     */
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   311
    protected Throwable(String message, Throwable cause,
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   312
                        boolean enableSuppression) {
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   313
        fillInStackTrace();
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   314
        detailMessage = message;
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   315
        this.cause = cause;
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   316
        if (!enableSuppression)
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   317
            suppressedExceptions = null;
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   318
    }
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   319
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   320
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns the detail message string of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   323
     * @return  the detail message string of this {@code Throwable} instance
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   324
     *          (which may be {@code null}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public String getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return detailMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Creates a localized description of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Subclasses may override this method in order to produce a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * locale-specific message.  For subclasses that do not override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * method, the default implementation returns the same result as
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   335
     * {@code getMessage()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return  The localized description of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public String getLocalizedMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   345
     * Returns the cause of this throwable or {@code null} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * cause is nonexistent or unknown.  (The cause is the throwable that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * caused this throwable to get thrown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <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
   350
     * the constructors requiring a {@code Throwable}, or that was set after
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * creation with the {@link #initCause(Throwable)} method.  While it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * typically unnecessary to override this method, a subclass can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * it to return a cause set by some other means.  This is appropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * a "legacy chained throwable" that predates the addition of chained
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   355
     * exceptions to {@code Throwable}.  Note that it is <i>not</i>
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   356
     * necessary to override any of the {@code PrintStackTrace} methods,
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   357
     * all of which invoke the {@code getCause} method to determine the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * cause of a throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   360
     * @return  the cause of this throwable or {@code null} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          cause is nonexistent or unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   364
    public synchronized Throwable getCause() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return (cause==this ? null : cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Initializes the <i>cause</i> of this throwable to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * (The cause is the throwable that caused this throwable to get thrown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>This method can be called at most once.  It is generally called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * within the constructor, or immediately after creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * throwable.  If this throwable was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * with {@link #Throwable(Throwable)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * {@link #Throwable(String,Throwable)}, this method cannot be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * even once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @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
   380
     *         {@link #getCause()} method).  (A {@code null} value is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *         permitted, and indicates that the cause is nonexistent or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *         unknown.)
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   383
     * @return  a reference to this {@code Throwable} instance.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   384
     * @throws IllegalArgumentException if {@code cause} is this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *         throwable.  (A throwable cannot be its own cause.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @throws IllegalStateException if this throwable was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         created with {@link #Throwable(Throwable)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *         {@link #Throwable(String,Throwable)}, or this method has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *         been called on this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public synchronized Throwable initCause(Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (this.cause != this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new IllegalStateException("Can't overwrite cause");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (cause == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw new IllegalArgumentException("Self-causation not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns a short description of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * The result is the concatenation of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <li> the {@linkplain Class#getName() name} of the class of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <li> ": " (a colon and a space)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <li> the result of invoking this object's {@link #getLocalizedMessage}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *      method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * </ul>
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   410
     * If {@code getLocalizedMessage} returns {@code null}, then just
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * the class name is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return a string representation of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        String s = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        String message = getLocalizedMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return (message != null) ? (s + ": " + message) : s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Prints this throwable and its backtrace to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * 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
   424
     * {@code Throwable} object on the error output stream that is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   425
     * the value of the field {@code System.err}. The first line of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * output contains the result of the {@link #toString()} method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * this object.  Remaining lines represent data previously recorded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * the method {@link #fillInStackTrace()}. The format of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * information depends on the implementation, but the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * example may be regarded as typical:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * java.lang.NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *         at MyClass.mash(MyClass.java:9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         at MyClass.crunch(MyClass.java:6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *         at MyClass.main(MyClass.java:3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * This example was produced by running the program:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * class MyClass {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *     public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *         crunch(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *     static void crunch(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         mash(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *     static void mash(int[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *         System.out.println(b[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * The backtrace for a throwable with an initialized, non-null cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * should generally include the backtrace for the cause.  The format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * of this information depends on the implementation, but the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * example may be regarded as typical:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * HighLevelException: MidLevelException: LowLevelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *         at Junk.a(Junk.java:13)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         at Junk.main(Junk.java:4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Caused by: MidLevelException: LowLevelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *         at Junk.c(Junk.java:23)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *         at Junk.b(Junk.java:17)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *         at Junk.a(Junk.java:11)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *         ... 1 more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Caused by: LowLevelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *         at Junk.e(Junk.java:30)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *         at Junk.d(Junk.java:27)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *         at Junk.c(Junk.java:21)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *         ... 3 more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * </pre>
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   470
     * Note the presence of lines containing the characters {@code "..."}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * These lines indicate that the remainder of the stack trace for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * exception matches the indicated number of frames from the bottom of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * stack trace of the exception that was caused by this exception (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * "enclosing" exception).  This shorthand can greatly reduce the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * of the output in the common case where a wrapped exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * from same method as the "causative exception" is caught.  The above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * example was produced by running the program:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * public class Junk {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *     public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *             a();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *         } catch(HighLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *             e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *     static void a() throws HighLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *             b();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *         } catch(MidLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *             throw new HighLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *     static void b() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *         c();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *     static void c() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *             d();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *         } catch(LowLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *             throw new MidLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *     static void d() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *        e();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *     static void e() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *         throw new LowLevelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * class HighLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *     HighLevelException(Throwable cause) { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * class MidLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *     MidLevelException(Throwable cause)  { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * class LowLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * </pre>
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   523
     * 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
   524
     * <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
   525
     * 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
   526
     * 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
   527
     * 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
   528
     * 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
   529
     * regarded as typical:
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   530
     *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   531
     * <pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   532
     * 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
   533
     *  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
   534
     *  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
   535
     *  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
   536
     *          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
   537
     *          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
   538
     *          ... 1 more
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   539
     * </pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   540
     * 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
   541
     * 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
   542
     * indented beyond their "containing exceptions."
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   543
     *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   544
     * <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
   545
     * exceptions:
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   546
     * <pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   547
     * 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
   548
     *  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
   549
     *  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
   550
     *          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
   551
     *          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
   552
     *  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
   553
     *          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
   554
     *          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
   555
     * 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
   556
     *  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
   557
     * </pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   558
     * 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
   559
     * <pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   560
     * 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
   561
     *  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
   562
     *  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
   563
     *          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
   564
     *          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
   565
     *  Caused by: java.lang.Exception: Rats, you caught me
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   566
     *          at Resource2$CloseFailException.<init>(Resource2.java:45)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   567
     *          ... 2 more
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   568
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public void printStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Prints this throwable and its backtrace to the specified print stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   577
     * @param s {@code PrintStream} to use for output
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    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
   580
        printStackTrace(new WrappedPrintStream(s));
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   581
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   582
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   583
    private void printStackTrace(PrintStreamOrWriter s) {
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   584
        // Guard against malicious overrides of Throwable.equals by
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   585
        // using a Set with identity equality semantics.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   586
        Set<Throwable> dejaVu =
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   587
            Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   588
        dejaVu.add(this);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   589
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   590
        synchronized (s.lock()) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   591
            // Print our stack trace
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            s.println(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            StackTraceElement[] trace = getOurStackTrace();
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   594
            for (StackTraceElement traceElement : trace)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   595
                s.println("\tat " + traceElement);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   597
            // Print suppressed exceptions, if any
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   598
            for (Throwable se : getSuppressed())
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   599
                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
   600
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   601
            // Print cause, if any
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            Throwable ourCause = getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            if (ourCause != null)
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   604
                ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   609
     * 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
   610
     * stack trace.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   612
    private void printEnclosedStackTrace(PrintStreamOrWriter s,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   613
                                         StackTraceElement[] enclosingTrace,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   614
                                         String caption,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   615
                                         String prefix,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   616
                                         Set<Throwable> dejaVu) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   617
        assert Thread.holdsLock(s.lock());
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   618
        if (dejaVu.contains(this)) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   619
            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
   620
        } else {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   621
            dejaVu.add(this);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   622
            // 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
   623
            StackTraceElement[] trace = getOurStackTrace();
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   624
            int m = trace.length - 1;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   625
            int n = enclosingTrace.length - 1;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   626
            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
   627
                m--; n--;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   628
            }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   629
            int framesInCommon = trace.length - 1 - m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   631
            // Print our stack trace
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   632
            s.println(prefix + caption + this);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   633
            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
   634
                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
   635
            if (framesInCommon != 0)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   636
                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
   637
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   638
            // Print suppressed exceptions, if any
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   639
            for (Throwable se : getSuppressed())
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   640
                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
   641
                                           prefix +"\t", dejaVu);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   642
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   643
            // Print cause, if any
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   644
            Throwable ourCause = getCause();
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   645
            if (ourCause != null)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   646
                ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Prints this throwable and its backtrace to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * print writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   654
     * @param s {@code PrintWriter} to use for output
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    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
   658
        printStackTrace(new WrappedPrintWriter(s));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   662
     * 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
   663
     * implementation of printStackTrace.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   665
    private abstract static class PrintStreamOrWriter {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   666
        /** 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
   667
        abstract Object lock();
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   668
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   669
        /** 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
   670
        abstract void println(Object o);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   671
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   673
    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
   674
        private final PrintStream printStream;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   675
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   676
        WrappedPrintStream(PrintStream printStream) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   677
            this.printStream = printStream;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   678
        }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   679
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   680
        Object lock() {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   681
            return printStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   683
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   684
        void println(Object o) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   685
            printStream.println(o);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   686
        }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   687
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   688
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   689
    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
   690
        private final PrintWriter printWriter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   692
        WrappedPrintWriter(PrintWriter printWriter) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   693
            this.printWriter = printWriter;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   694
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   696
        Object lock() {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   697
            return printWriter;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   698
        }
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
        void println(Object o) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   701
            printWriter.println(o);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   702
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * 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
   707
     * {@code Throwable} object information about the current state of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * the stack frames for the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   710
     * @return  a reference to this {@code Throwable} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @see     java.lang.Throwable#printStackTrace()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    public synchronized native Throwable fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Provides programmatic access to the stack trace information printed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * {@link #printStackTrace()}.  Returns an array of stack trace elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * each representing one stack frame.  The zeroth element of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * (assuming the array's length is non-zero) represents the top of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * stack, which is the last method invocation in the sequence.  Typically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * this is the point at which this throwable was created and thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * The last element of the array (assuming the array's length is non-zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * represents the bottom of the stack, which is the first method invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * <p>Some virtual machines may, under some circumstances, omit one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * or more stack frames from the stack trace.  In the extreme case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * a virtual machine that has no stack trace information concerning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * this throwable is permitted to return a zero-length array from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * method.  Generally speaking, the array returned by this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * contain one element for every frame that would be printed by
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   732
     * {@code printStackTrace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @return an array of stack trace elements representing the stack trace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *         pertaining to this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    public StackTraceElement[] getStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return getOurStackTrace().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    private synchronized StackTraceElement[] getOurStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // Initialize stack trace if this is the first call to this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (stackTrace == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            int depth = getStackTraceDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            stackTrace = new StackTraceElement[depth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            for (int i=0; i < depth; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                stackTrace[i] = getStackTraceElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        return stackTrace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * Sets the stack trace elements that will be returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * {@link #getStackTrace()} and printed by {@link #printStackTrace()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * and related methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * This method, which is designed for use by RPC frameworks and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * advanced systems, allows the client to override the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * stack trace that is either generated by {@link #fillInStackTrace()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * when a throwable is constructed or deserialized when a throwable is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * read from a serialization stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param   stackTrace the stack trace elements to be associated with
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   765
     * this {@code Throwable}.  The specified array is copied by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * call; changes in the specified array after the method invocation
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   767
     * returns will have no affect on this {@code Throwable}'s stack
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   770
     * @throws NullPointerException if {@code stackTrace} is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   771
     *         {@code null}, or if any of the elements of
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   772
     *         {@code stackTrace} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    public void setStackTrace(StackTraceElement[] stackTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        StackTraceElement[] defensiveCopy = stackTrace.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        for (int i = 0; i < defensiveCopy.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (defensiveCopy[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                throw new NullPointerException("stackTrace[" + i + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   782
        synchronized (this) {
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   783
            this.stackTrace = defensiveCopy;
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   784
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Returns the number of elements in the stack trace (or 0 if the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * trace is unavailable).
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   790
     *
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   791
     * package-protection for use by SharedSecrets.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   793
    native int getStackTraceDepth();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Returns the specified element of the stack trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   798
     * package-protection for use by SharedSecrets.
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   799
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @param index index of the element to return.
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   801
     * @throws IndexOutOfBoundsException if {@code index < 0 ||
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   802
     *         index >= getStackTraceDepth() }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     */
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   804
    native StackTraceElement getStackTraceElement(int index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   806
    /**
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 7803
diff changeset
   807
     * Reads a {@code Throwable} from a stream, enforcing
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   808
     * well-formedness constraints on fields.  Null entries and
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   809
     * self-pointers are not allowed in the list of {@code
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   810
     * suppressedExceptions}.  Null entries are not allowed for stack
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   811
     * trace elements.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   812
     *
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   813
     * Note that there are no constraints on the value the {@code
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   814
     * cause} field can hold; both {@code null} and {@code this} are
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   815
     * valid values for the field.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   816
     */
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   817
    private void readObject(ObjectInputStream s)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   818
        throws IOException, ClassNotFoundException {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   819
        s.defaultReadObject();     // read in all fields
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   820
        if (suppressedExceptions != null) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   821
            List<Throwable> suppressed = null;
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   822
            if (suppressedExceptions.isEmpty()) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   823
                // Use the sentinel for a zero-length list
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   824
                suppressed = SUPPRESSED_SENTINEL;
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   825
            } 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
   826
                suppressed = new ArrayList<>(1);
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   827
                for (Throwable t : suppressedExceptions) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   828
                    // Enforce constraints on suppressed exceptions in
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   829
                    // case of corrupt or malicious stream.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   830
                    if (t == null)
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   831
                        throw new NullPointerException(NULL_CAUSE_MESSAGE);
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   832
                    if (t == this)
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   833
                        throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   834
                    suppressed.add(t);
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   835
                }
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   836
            }
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   837
            suppressedExceptions = suppressed;
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   838
        } // else a null suppressedExceptions field remains null
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   839
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   840
        if (stackTrace != null) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   841
            for (StackTraceElement ste : stackTrace) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   842
                if (ste == null)
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   843
                    throw new NullPointerException("null StackTraceElement in serial stream. ");
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   844
            }
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   845
        } else {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   846
            // A null stackTrace field in the serial form can result from
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   847
            // an exception serialized without that field in older JDK releases.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   848
            stackTrace = EMPTY_STACK;
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   849
        }
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   850
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   851
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   852
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   853
    /**
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   854
     * Write a {@code Throwable} object to a stream.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   855
     */
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   856
    private synchronized void writeObject(ObjectOutputStream s)
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   857
        throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        getOurStackTrace();  // Ensure that stackTrace field is initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   861
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   862
    /**
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 7803
diff changeset
   863
     * Appends the specified exception to the exceptions that were
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 7803
diff changeset
   864
     * suppressed in order to deliver this exception. This method is
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 7803
diff changeset
   865
     * typically called (automatically and implicitly) by the {@code
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 7803
diff changeset
   866
     * try}-with-resources statement.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   867
     *
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   868
     * <p>The suppression behavior is enabled <em>unless</em> disabled
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   869
     * {@linkplain #Throwable(String, Throwable, boolean) via a
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   870
     * constructor}.  When suppression is disabled, this method does
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   871
     * nothing other than to validate its argument.
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   872
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   873
     * <p>Note that when one exception {@linkplain
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   874
     * #initCause(Throwable) causes} another exception, the first
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   875
     * exception is usually caught and then the second exception is
9005
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   876
     * thrown in response.  In other words, there is a causal
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   877
     * connection between the two exceptions.
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   878
     *
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   879
     * In contrast, there are situations where two independent
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   880
     * exceptions can be thrown in sibling code blocks, in particular
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   881
     * in the {@code try} block of a {@code try}-with-resources
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   882
     * statement and the compiler-generated {@code finally} block
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   883
     * which closes the resource.
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   884
     *
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   885
     * In these situations, only one of the thrown exceptions can be
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   886
     * propagated.  In the {@code try}-with-resources statement, when
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   887
     * there are two such exceptions, the exception originating from
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   888
     * the {@code try} block is propagated and the exception from the
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   889
     * {@code finally} block is added to the list of exceptions
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   890
     * suppressed by the exception from the {@code try} block.  As an
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   891
     * exception unwinds the stack, it can accumulate multiple
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   892
     * suppressed exceptions.
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   893
     *
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   894
     * <p>An exception may have suppressed exceptions while also being
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   895
     * caused by another exception.  Whether or not an exception has a
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   896
     * cause is semantically known at the time of its creation, unlike
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   897
     * whether or not an exception will suppress other exceptions
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   898
     * which is typically only determined after an exception is
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   899
     * thrown.
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   900
     *
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   901
     * <p>Note that programmer written code is also able to take
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   902
     * advantage of calling this method in situations where there are
a329d2b300c9 7031371: Clarify javadoc of Throwable, including addSuppressed
darcy
parents: 7988
diff changeset
   903
     * multiple sibling exceptions and only one can be propagated.
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   904
     *
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   905
     * @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
   906
     *        suppressed exceptions
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   907
     * @throws IllegalArgumentException if {@code exception} is this
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   908
     *         throwable; a throwable cannot suppress itself.
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   909
     * @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
   910
     * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   911
     */
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   912
    public final synchronized void addSuppressed(Throwable exception) {
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   913
        if (exception == this)
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   914
            throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   915
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   916
        if (exception == null)
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   917
            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
   918
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   919
        if (suppressedExceptions == null) // Suppressed exceptions not recorded
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   920
            return;
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   921
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   922
        if (suppressedExceptions == SUPPRESSED_SENTINEL)
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   923
            suppressedExceptions = new ArrayList<>(1);
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   924
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   925
        suppressedExceptions.add(exception);
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   926
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   927
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   928
    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
   929
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   930
    /**
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   931
     * 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
   932
     * 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
   933
     * 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
   934
     *
9020
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   935
     * If no exceptions were suppressed or {@linkplain
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   936
     * Throwable(String, Throwable, boolean) suppression is disabled},
13b639abc930 7005628: Clarify NPE behavior of Throwable.addSuppressed(null)
darcy
parents: 9005
diff changeset
   937
     * an empty array is returned.
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   938
     *
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   939
     * @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
   940
     *         suppressed to deliver this exception.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   941
     * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   942
     */
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   943
    public final synchronized Throwable[] getSuppressed() {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   944
        if (suppressedExceptions == SUPPRESSED_SENTINEL ||
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 6515
diff changeset
   945
            suppressedExceptions == null)
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   946
            return EMPTY_THROWABLE_ARRAY;
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   947
        else
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   948
            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
   949
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
}