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