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