jdk/src/share/classes/java/lang/Throwable.java
author darcy
Wed, 25 Aug 2010 15:35:45 -0700
changeset 6515 7a5ccc90b436
parent 6302 ec168532bf3f
child 7186 7f5fe8985263
permissions -rw-r--r--
6980019: Finish rename of ARM -> try-with-resources in jdk repository Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4666
diff changeset
     2
 * Copyright (c) 1994, 2006, 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * The throwable that caused this throwable to get thrown, or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * throwable was not caused by another throwable, or if the causative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * throwable is unknown.  If this field is equal to this throwable itself,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * it indicates that the cause of this throwable has not yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private Throwable cause = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * The stack trace, as returned by {@link #getStackTrace()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private StackTraceElement[] stackTrace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * This field is lazily initialized on first use or serialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * nulled out when fillInStackTrace is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   197
     * The list of suppressed exceptions, as returned by
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   198
     * {@link #getSuppressedExceptions()}.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   199
     *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   200
     * @serial
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   201
     * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   202
     */
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   203
    private List<Throwable> suppressedExceptions = null;
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   204
    /*
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   205
     * This field is lazily initialized when the first suppressed
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   206
     * exception is added.
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   207
     *
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   208
     * OutOfMemoryError is preallocated in the VM for better OOM
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   209
     * diagnosability during VM initialization. Constructor can't
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   210
     * be not invoked. If a new field to be added in the future must
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   211
     * be initialized to non-null, it requires a synchronized VM change.
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   212
     */
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   213
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   214
    /** 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
   215
    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
   216
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   217
    /** 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
   218
    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
   219
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   220
    /** 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
   221
    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
   222
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   223
    /**
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   224
     * Constructs a new throwable with {@code null} as its detail message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * The cause is not initialized, and may subsequently be initialized by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * call to {@link #initCause}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public Throwable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Constructs a new throwable with the specified detail message.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * cause is not initialized, and may subsequently be initialized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * a call to {@link #initCause}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param   message   the detail message. The detail message is saved for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *          later retrieval by the {@link #getMessage()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public Throwable(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        detailMessage = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Constructs a new throwable with the specified detail message and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * cause.  <p>Note that the detail message associated with
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   254
     * {@code cause} is <i>not</i> automatically incorporated in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * this throwable's detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param  message the detail message (which is saved for later retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *         by the {@link #getMessage()} method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @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
   263
     *         {@link #getCause()} method).  (A {@code null} value is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *         permitted, and indicates that the cause is nonexistent or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *         unknown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public Throwable(String message, Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        detailMessage = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Constructs a new throwable with the specified cause and a detail
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   276
     * message of {@code (cause==null ? null : cause.toString())} (which
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   277
     * typically contains the class and detail message of {@code cause}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * This constructor is useful for throwables that are little more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * wrappers for other throwables (for example, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * java.security.PrivilegedActionException}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p>The {@link #fillInStackTrace()} method is called to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * the stack trace data in the newly created throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param  cause the cause (which is saved for later retrieval by the
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   286
     *         {@link #getCause()} method).  (A {@code null} value is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         permitted, and indicates that the cause is nonexistent or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *         unknown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public Throwable(Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        detailMessage = (cause==null ? null : cause.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Returns the detail message string of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   300
     * @return  the detail message string of this {@code Throwable} instance
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   301
     *          (which may be {@code null}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public String getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return detailMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Creates a localized description of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Subclasses may override this method in order to produce a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * locale-specific message.  For subclasses that do not override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * method, the default implementation returns the same result as
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   312
     * {@code getMessage()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return  The localized description of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public String getLocalizedMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   322
     * Returns the cause of this throwable or {@code null} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * cause is nonexistent or unknown.  (The cause is the throwable that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * caused this throwable to get thrown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <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
   327
     * the constructors requiring a {@code Throwable}, or that was set after
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * creation with the {@link #initCause(Throwable)} method.  While it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * typically unnecessary to override this method, a subclass can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * it to return a cause set by some other means.  This is appropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * a "legacy chained throwable" that predates the addition of chained
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   332
     * exceptions to {@code Throwable}.  Note that it is <i>not</i>
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   333
     * necessary to override any of the {@code PrintStackTrace} methods,
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   334
     * all of which invoke the {@code getCause} method to determine the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * cause of a throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   337
     * @return  the cause of this throwable or {@code null} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          cause is nonexistent or unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   341
    public synchronized Throwable getCause() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return (cause==this ? null : cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Initializes the <i>cause</i> of this throwable to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * (The cause is the throwable that caused this throwable to get thrown.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <p>This method can be called at most once.  It is generally called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * within the constructor, or immediately after creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * throwable.  If this throwable was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * with {@link #Throwable(Throwable)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * {@link #Throwable(String,Throwable)}, this method cannot be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * even once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @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
   357
     *         {@link #getCause()} method).  (A {@code null} value is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *         permitted, and indicates that the cause is nonexistent or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *         unknown.)
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   360
     * @return  a reference to this {@code Throwable} instance.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   361
     * @throws IllegalArgumentException if {@code cause} is this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *         throwable.  (A throwable cannot be its own cause.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws IllegalStateException if this throwable was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *         created with {@link #Throwable(Throwable)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         {@link #Throwable(String,Throwable)}, or this method has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *         been called on this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public synchronized Throwable initCause(Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (this.cause != this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new IllegalStateException("Can't overwrite cause");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (cause == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            throw new IllegalArgumentException("Self-causation not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Returns a short description of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * The result is the concatenation of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <li> the {@linkplain Class#getName() name} of the class of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <li> ": " (a colon and a space)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <li> the result of invoking this object's {@link #getLocalizedMessage}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *      method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * </ul>
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   387
     * If {@code getLocalizedMessage} returns {@code null}, then just
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * the class name is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return a string representation of this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        String s = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        String message = getLocalizedMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return (message != null) ? (s + ": " + message) : s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Prints this throwable and its backtrace to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * 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
   401
     * {@code Throwable} object on the error output stream that is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   402
     * the value of the field {@code System.err}. The first line of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * output contains the result of the {@link #toString()} method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * this object.  Remaining lines represent data previously recorded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the method {@link #fillInStackTrace()}. The format of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * information depends on the implementation, but the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * example may be regarded as typical:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * java.lang.NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *         at MyClass.mash(MyClass.java:9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *         at MyClass.crunch(MyClass.java:6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *         at MyClass.main(MyClass.java:3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * This example was produced by running the program:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * class MyClass {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *     public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         crunch(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *     static void crunch(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *         mash(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *     static void mash(int[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *         System.out.println(b[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * The backtrace for a throwable with an initialized, non-null cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * should generally include the backtrace for the cause.  The format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * of this information depends on the implementation, but the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * example may be regarded as typical:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * HighLevelException: MidLevelException: LowLevelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         at Junk.a(Junk.java:13)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *         at Junk.main(Junk.java:4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Caused by: MidLevelException: LowLevelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *         at Junk.c(Junk.java:23)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *         at Junk.b(Junk.java:17)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *         at Junk.a(Junk.java:11)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *         ... 1 more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Caused by: LowLevelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *         at Junk.e(Junk.java:30)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         at Junk.d(Junk.java:27)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         at Junk.c(Junk.java:21)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *         ... 3 more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * </pre>
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   447
     * Note the presence of lines containing the characters {@code "..."}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * These lines indicate that the remainder of the stack trace for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * exception matches the indicated number of frames from the bottom of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * stack trace of the exception that was caused by this exception (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * "enclosing" exception).  This shorthand can greatly reduce the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * of the output in the common case where a wrapped exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * from same method as the "causative exception" is caught.  The above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * example was produced by running the program:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * public class Junk {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *     public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *             a();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *         } catch(HighLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *             e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *     static void a() throws HighLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *             b();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *         } catch(MidLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *             throw new HighLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *     static void b() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *         c();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *     static void c() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *             d();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *         } catch(LowLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *             throw new MidLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *     static void d() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *        e();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *     static void e() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *         throw new LowLevelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * class HighLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *     HighLevelException(Throwable cause) { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * class MidLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *     MidLevelException(Throwable cause)  { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * class LowLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * </pre>
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   500
     * 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
   501
     * <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
   502
     * 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
   503
     * 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
   504
     * 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
   505
     * 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
   506
     * regarded as typical:
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   507
     *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   508
     * <pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   509
     * 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
   510
     *  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
   511
     *  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
   512
     *  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
   513
     *          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
   514
     *          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
   515
     *          ... 1 more
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   516
     * </pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   517
     * 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
   518
     * 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
   519
     * indented beyond their "containing exceptions."
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   520
     *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   521
     * <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
   522
     * exceptions:
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   523
     * <pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   524
     * 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
   525
     *  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
   526
     *  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
   527
     *          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
   528
     *          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
   529
     *  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
   530
     *          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
   531
     *          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
   532
     * 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
   533
     *  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
   534
     * </pre>
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   535
     * 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
   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: Main block
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   538
     *  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
   539
     *  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
   540
     *          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
   541
     *          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
   542
     *  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
   543
     *          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
   544
     *          ... 2 more
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   545
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public void printStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Prints this throwable and its backtrace to the specified print stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   554
     * @param s {@code PrintStream} to use for output
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    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
   557
        printStackTrace(new WrappedPrintStream(s));
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   558
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   559
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   560
    private void printStackTrace(PrintStreamOrWriter s) {
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   561
        // Guard against malicious overrides of Throwable.equals by
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   562
        // using a Set with identity equality semantics.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   563
        Set<Throwable> dejaVu =
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   564
            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
   565
        dejaVu.add(this);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   566
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   567
        synchronized (s.lock()) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   568
            // Print our stack trace
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            s.println(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            StackTraceElement[] trace = getOurStackTrace();
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   571
            for (StackTraceElement traceElement : trace)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   572
                s.println("\tat " + traceElement);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   574
            // Print suppressed exceptions, if any
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   575
            for (Throwable se : getSuppressedExceptions())
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   576
                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
   577
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   578
            // Print cause, if any
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            Throwable ourCause = getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            if (ourCause != null)
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   581
                ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   586
     * 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
   587
     * stack trace.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   589
    private void printEnclosedStackTrace(PrintStreamOrWriter s,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   590
                                         StackTraceElement[] enclosingTrace,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   591
                                         String caption,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   592
                                         String prefix,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   593
                                         Set<Throwable> dejaVu) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   594
        assert Thread.holdsLock(s.lock());
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   595
        if (dejaVu.contains(this)) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   596
            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
   597
        } else {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   598
            dejaVu.add(this);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   599
            // 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
   600
            StackTraceElement[] trace = getOurStackTrace();
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   601
            int m = trace.length - 1;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   602
            int n = enclosingTrace.length - 1;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   603
            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
   604
                m--; n--;
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
            int framesInCommon = trace.length - 1 - m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   608
            // Print our stack trace
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   609
            s.println(prefix + caption + this);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   610
            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
   611
                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
   612
            if (framesInCommon != 0)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   613
                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
   614
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   615
            // Print suppressed exceptions, if any
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   616
            for (Throwable se : getSuppressedExceptions())
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   617
                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
   618
                                           prefix +"\t", dejaVu);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   619
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   620
            // Print cause, if any
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   621
            Throwable ourCause = getCause();
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   622
            if (ourCause != null)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   623
                ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Prints this throwable and its backtrace to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * print writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   631
     * @param s {@code PrintWriter} to use for output
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    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
   635
        printStackTrace(new WrappedPrintWriter(s));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   639
     * 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
   640
     * implementation of printStackTrace.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   642
    private abstract static class PrintStreamOrWriter {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   643
        /** 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
   644
        abstract Object lock();
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   645
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   646
        /** 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
   647
        abstract void println(Object o);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   648
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   650
    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
   651
        private final PrintStream printStream;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   652
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   653
        WrappedPrintStream(PrintStream printStream) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   654
            this.printStream = printStream;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   655
        }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   656
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   657
        Object lock() {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   658
            return printStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   660
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   661
        void println(Object o) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   662
            printStream.println(o);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   663
        }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   664
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   665
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   666
    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
   667
        private final PrintWriter printWriter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   669
        WrappedPrintWriter(PrintWriter printWriter) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   670
            this.printWriter = printWriter;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   671
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   673
        Object lock() {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   674
            return printWriter;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   675
        }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   676
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   677
        void println(Object o) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   678
            printWriter.println(o);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   679
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * 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
   684
     * {@code Throwable} object information about the current state of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * the stack frames for the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   687
     * @return  a reference to this {@code Throwable} instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @see     java.lang.Throwable#printStackTrace()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public synchronized native Throwable fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Provides programmatic access to the stack trace information printed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * {@link #printStackTrace()}.  Returns an array of stack trace elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * each representing one stack frame.  The zeroth element of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * (assuming the array's length is non-zero) represents the top of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * stack, which is the last method invocation in the sequence.  Typically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * this is the point at which this throwable was created and thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * The last element of the array (assuming the array's length is non-zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * represents the bottom of the stack, which is the first method invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <p>Some virtual machines may, under some circumstances, omit one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * or more stack frames from the stack trace.  In the extreme case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * a virtual machine that has no stack trace information concerning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * this throwable is permitted to return a zero-length array from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * method.  Generally speaking, the array returned by this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * 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
   709
     * {@code printStackTrace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @return an array of stack trace elements representing the stack trace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *         pertaining to this throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public StackTraceElement[] getStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return getOurStackTrace().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    private synchronized StackTraceElement[] getOurStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        // Initialize stack trace if this is the first call to this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (stackTrace == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            int depth = getStackTraceDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            stackTrace = new StackTraceElement[depth];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            for (int i=0; i < depth; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                stackTrace[i] = getStackTraceElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return stackTrace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Sets the stack trace elements that will be returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * {@link #getStackTrace()} and printed by {@link #printStackTrace()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * and related methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * This method, which is designed for use by RPC frameworks and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * advanced systems, allows the client to override the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * stack trace that is either generated by {@link #fillInStackTrace()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * when a throwable is constructed or deserialized when a throwable is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * read from a serialization stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @param   stackTrace the stack trace elements to be associated with
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   742
     * this {@code Throwable}.  The specified array is copied by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * call; changes in the specified array after the method invocation
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   744
     * returns will have no affect on this {@code Throwable}'s stack
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   747
     * @throws NullPointerException if {@code stackTrace} is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   748
     *         {@code null}, or if any of the elements of
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   749
     *         {@code stackTrace} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public void setStackTrace(StackTraceElement[] stackTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        StackTraceElement[] defensiveCopy = stackTrace.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        for (int i = 0; i < defensiveCopy.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (defensiveCopy[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                throw new NullPointerException("stackTrace[" + i + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   759
        synchronized (this) {
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   760
            this.stackTrace = defensiveCopy;
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   761
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * Returns the number of elements in the stack trace (or 0 if the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * trace is unavailable).
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   767
     *
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   768
     * package-protection for use by SharedSecrets.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   770
    native int getStackTraceDepth();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Returns the specified element of the stack trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   775
     * package-protection for use by SharedSecrets.
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   776
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param index index of the element to return.
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   778
     * @throws IndexOutOfBoundsException if {@code index < 0 ||
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   779
     *         index >= getStackTraceDepth() }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2
diff changeset
   781
    native StackTraceElement getStackTraceElement(int index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   783
    private void readObject(ObjectInputStream s)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   784
        throws IOException, ClassNotFoundException {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   785
        s.defaultReadObject();     // read in all fields
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   786
        List<Throwable> suppressed = null;
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   787
        if (suppressedExceptions != null &&
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   788
            !suppressedExceptions.isEmpty()) { // Copy Throwables to new list
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   789
            suppressed = new ArrayList<Throwable>();
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   790
            for (Throwable t : suppressedExceptions) {
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   791
                if (t == null)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   792
                    throw new NullPointerException(NULL_CAUSE_MESSAGE);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   793
                suppressed.add(t);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   794
            }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   795
        }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   796
        suppressedExceptions = suppressed;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   797
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   798
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   799
    private synchronized void writeObject(ObjectOutputStream s)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        getOurStackTrace();  // Ensure that stackTrace field is initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   805
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   806
    /**
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   807
     * Adds the specified exception to the list of exceptions that
6515
7a5ccc90b436 6980019: Finish rename of ARM -> try-with-resources in jdk repository
darcy
parents: 6302
diff changeset
   808
     * were 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
   809
     * 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
   810
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   811
     * <p>Note that when one exception {@linkplain
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   812
     * #initCause(Throwable) causes} another exception, the first
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   813
     * exception is usually caught and then the second exception is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   814
     * thrown in response.  In contrast, when one exception suppresses
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   815
     * another, two exceptions are thrown in sibling code blocks, such
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   816
     * 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
   817
     * control flow can only continue with one exception so the second
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   818
     * is recorded as a suppressed exception of the first.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   819
     *
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   820
     * @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
   821
     *        suppressed exceptions
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   822
     * @throws NullPointerException if {@code exception} is null
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   823
     * @throws IllegalArgumentException if {@code exception} is this
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   824
     *         throwable; a throwable cannot suppress itself.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   825
     * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   826
     */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   827
    public synchronized void addSuppressedException(Throwable exception) {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   828
        if (exception == null)
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   829
            throw new NullPointerException(NULL_CAUSE_MESSAGE);
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   830
        if (exception == this)
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
   831
            throw new IllegalArgumentException("Self-suppression not permitted");
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   832
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   833
        if (suppressedExceptions == null)
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   834
            suppressedExceptions = new ArrayList<Throwable>();
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   835
        suppressedExceptions.add(exception);
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   836
    }
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   837
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   838
    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
   839
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   840
    /**
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   841
     * 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
   842
     * 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
   843
     * 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
   844
     *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   845
     * @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
   846
     *         suppressed to deliver this exception.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   847
     * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents: 5506
diff changeset
   848
     */
6302
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   849
    public synchronized Throwable[] getSuppressedExceptions() {
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   850
        if (suppressedExceptions == null)
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   851
            return EMPTY_THROWABLE_ARRAY;
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   852
        else
ec168532bf3f 6973831: NPE when printing stack trace of OOME
mchung
parents: 6110
diff changeset
   853
            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
   854
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
}