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