2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2007-12-01 00:00:00 +00:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package java.lang;
|
|
|
|
import java.io.*;
|
2010-06-23 17:03:40 -07:00
|
|
|
import java.util.*;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
/**
|
2010-07-15 18:02:05 -07:00
|
|
|
* The {@code Throwable} class is the superclass of all errors and
|
2007-12-01 00:00:00 +00:00
|
|
|
* exceptions in the Java language. Only objects that are instances of this
|
|
|
|
* class (or one of its subclasses) are thrown by the Java Virtual Machine or
|
2010-07-15 18:02:05 -07:00
|
|
|
* can be thrown by the Java {@code throw} statement. Similarly, only
|
2007-12-01 00:00:00 +00:00
|
|
|
* this class or one of its subclasses can be the argument type in a
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code catch} clause.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
2010-01-07 19:42:43 -08:00
|
|
|
* For the purposes of compile-time checking of exceptions, {@code
|
|
|
|
* Throwable} and any subclass of {@code Throwable} that is not also a
|
|
|
|
* subclass of either {@link RuntimeException} or {@link Error} are
|
|
|
|
* regarded as checked exceptions.
|
|
|
|
*
|
2007-12-01 00:00:00 +00:00
|
|
|
* <p>Instances of two subclasses, {@link java.lang.Error} and
|
|
|
|
* {@link java.lang.Exception}, are conventionally used to indicate
|
|
|
|
* that exceptional situations have occurred. Typically, these instances
|
|
|
|
* are freshly created in the context of the exceptional situation so
|
|
|
|
* as to include relevant information (such as stack trace data).
|
|
|
|
*
|
|
|
|
* <p>A throwable contains a snapshot of the execution stack of its thread at
|
|
|
|
* the time it was created. It can also contain a message string that gives
|
|
|
|
* more information about the error. Finally, it can contain a <i>cause</i>:
|
|
|
|
* another throwable that caused this throwable to get thrown. The cause
|
|
|
|
* facility is new in release 1.4. It is also known as the <i>chained
|
|
|
|
* exception</i> facility, as the cause can, itself, have a cause, and so on,
|
|
|
|
* leading to a "chain" of exceptions, each caused by another.
|
|
|
|
*
|
|
|
|
* <p>One reason that a throwable may have a cause is that the class that
|
|
|
|
* throws it is built atop a lower layered abstraction, and an operation on
|
|
|
|
* the upper layer fails due to a failure in the lower layer. It would be bad
|
|
|
|
* design to let the throwable thrown by the lower layer propagate outward, as
|
|
|
|
* it is generally unrelated to the abstraction provided by the upper layer.
|
|
|
|
* Further, doing so would tie the API of the upper layer to the details of
|
|
|
|
* its implementation, assuming the lower layer's exception was a checked
|
|
|
|
* exception. Throwing a "wrapped exception" (i.e., an exception containing a
|
|
|
|
* cause) allows the upper layer to communicate the details of the failure to
|
|
|
|
* its caller without incurring either of these shortcomings. It preserves
|
|
|
|
* the flexibility to change the implementation of the upper layer without
|
|
|
|
* changing its API (in particular, the set of exceptions thrown by its
|
|
|
|
* methods).
|
|
|
|
*
|
|
|
|
* <p>A second reason that a throwable may have a cause is that the method
|
|
|
|
* that throws it must conform to a general-purpose interface that does not
|
|
|
|
* permit the method to throw the cause directly. For example, suppose
|
|
|
|
* a persistent collection conforms to the {@link java.util.Collection
|
|
|
|
* Collection} interface, and that its persistence is implemented atop
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code java.io}. Suppose the internals of the {@code add} method
|
2007-12-01 00:00:00 +00:00
|
|
|
* can throw an {@link java.io.IOException IOException}. The implementation
|
2010-07-15 18:02:05 -07:00
|
|
|
* can communicate the details of the {@code IOException} to its caller
|
|
|
|
* while conforming to the {@code Collection} interface by wrapping the
|
|
|
|
* {@code IOException} in an appropriate unchecked exception. (The
|
2007-12-01 00:00:00 +00:00
|
|
|
* specification for the persistent collection should indicate that it is
|
|
|
|
* capable of throwing such exceptions.)
|
|
|
|
*
|
|
|
|
* <p>A cause can be associated with a throwable in two ways: via a
|
|
|
|
* constructor that takes the cause as an argument, or via the
|
|
|
|
* {@link #initCause(Throwable)} method. New throwable classes that
|
|
|
|
* wish to allow causes to be associated with them should provide constructors
|
|
|
|
* that take a cause and delegate (perhaps indirectly) to one of the
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code Throwable} constructors that takes a cause. For example:
|
2007-12-01 00:00:00 +00:00
|
|
|
* <pre>
|
|
|
|
* try {
|
|
|
|
* lowLevelOp();
|
|
|
|
* } catch (LowLevelException le) {
|
|
|
|
* throw new HighLevelException(le); // Chaining-aware constructor
|
|
|
|
* }
|
|
|
|
* </pre>
|
2010-07-15 18:02:05 -07:00
|
|
|
* Because the {@code initCause} method is public, it allows a cause to be
|
2007-12-01 00:00:00 +00:00
|
|
|
* associated with any throwable, even a "legacy throwable" whose
|
|
|
|
* implementation predates the addition of the exception chaining mechanism to
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code Throwable}. For example:
|
2007-12-01 00:00:00 +00:00
|
|
|
* <pre>
|
|
|
|
* try {
|
|
|
|
* lowLevelOp();
|
|
|
|
* } catch (LowLevelException le) {
|
|
|
|
* throw (HighLevelException)
|
2010-06-23 17:03:40 -07:00
|
|
|
* new HighLevelException().initCause(le); // Legacy constructor
|
2007-12-01 00:00:00 +00:00
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <p>Prior to release 1.4, there were many throwables that had their own
|
|
|
|
* non-standard exception chaining mechanisms (
|
|
|
|
* {@link ExceptionInInitializerError}, {@link ClassNotFoundException},
|
|
|
|
* {@link java.lang.reflect.UndeclaredThrowableException},
|
|
|
|
* {@link java.lang.reflect.InvocationTargetException},
|
|
|
|
* {@link java.io.WriteAbortedException},
|
|
|
|
* {@link java.security.PrivilegedActionException},
|
|
|
|
* {@link java.awt.print.PrinterIOException},
|
|
|
|
* {@link java.rmi.RemoteException} and
|
|
|
|
* {@link javax.naming.NamingException}).
|
|
|
|
* All of these throwables have been retrofitted to
|
|
|
|
* use the standard exception chaining mechanism, while continuing to
|
|
|
|
* implement their "legacy" chaining mechanisms for compatibility.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* <p>Further, as of release 1.4, many general purpose {@code Throwable}
|
2007-12-01 00:00:00 +00:00
|
|
|
* classes (for example {@link Exception}, {@link RuntimeException},
|
|
|
|
* {@link Error}) have been retrofitted with constructors that take
|
|
|
|
* a cause. This was not strictly necessary, due to the existence of the
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code initCause} method, but it is more convenient and expressive to
|
2007-12-01 00:00:00 +00:00
|
|
|
* delegate to a constructor that takes a cause.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* <p>By convention, class {@code Throwable} and its subclasses have two
|
2007-12-01 00:00:00 +00:00
|
|
|
* constructors, one that takes no arguments and one that takes a
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code String} argument that can be used to produce a detail message.
|
2007-12-01 00:00:00 +00:00
|
|
|
* Further, those subclasses that might likely have a cause associated with
|
|
|
|
* them should have two more constructors, one that takes a
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code Throwable} (the cause), and one that takes a
|
|
|
|
* {@code String} (the detail message) and a {@code Throwable} (the
|
2007-12-01 00:00:00 +00:00
|
|
|
* cause).
|
|
|
|
*
|
|
|
|
* <p>Also introduced in release 1.4 is the {@link #getStackTrace()} method,
|
|
|
|
* which allows programmatic access to the stack trace information that was
|
|
|
|
* previously available only in text form, via the various forms of the
|
|
|
|
* {@link #printStackTrace()} method. This information has been added to the
|
2010-07-15 18:02:05 -07:00
|
|
|
* <i>serialized representation</i> of this class so {@code getStackTrace}
|
|
|
|
* and {@code printStackTrace} will operate properly on a throwable that
|
2007-12-01 00:00:00 +00:00
|
|
|
* was obtained by deserialization.
|
|
|
|
*
|
|
|
|
* @author unascribed
|
|
|
|
* @author Josh Bloch (Added exception chaining and programmatic access to
|
|
|
|
* stack trace in 1.4.)
|
2010-01-07 19:42:43 -08:00
|
|
|
* @jls3 11.2 Compile-Time Checking of Exceptions
|
2007-12-01 00:00:00 +00:00
|
|
|
* @since JDK1.0
|
|
|
|
*/
|
|
|
|
public class Throwable implements Serializable {
|
|
|
|
/** use serialVersionUID from JDK 1.0.2 for interoperability */
|
|
|
|
private static final long serialVersionUID = -3042686055658047285L;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Native code saves some indication of the stack backtrace in this slot.
|
|
|
|
*/
|
|
|
|
private transient Object backtrace;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specific details about the Throwable. For example, for
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code FileNotFoundException}, this contains the name of
|
2007-12-01 00:00:00 +00:00
|
|
|
* the file that could not be found.
|
|
|
|
*
|
|
|
|
* @serial
|
|
|
|
*/
|
|
|
|
private String detailMessage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The throwable that caused this throwable to get thrown, or null if this
|
|
|
|
* throwable was not caused by another throwable, or if the causative
|
|
|
|
* throwable is unknown. If this field is equal to this throwable itself,
|
|
|
|
* it indicates that the cause of this throwable has not yet been
|
|
|
|
* initialized.
|
|
|
|
*
|
|
|
|
* @serial
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
private Throwable cause = this;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The stack trace, as returned by {@link #getStackTrace()}.
|
|
|
|
*
|
|
|
|
* @serial
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
private StackTraceElement[] stackTrace;
|
|
|
|
/*
|
|
|
|
* This field is lazily initialized on first use or serialization and
|
|
|
|
* nulled out when fillInStackTrace is called.
|
|
|
|
*/
|
|
|
|
|
2010-06-23 17:03:40 -07:00
|
|
|
/**
|
|
|
|
* The list of suppressed exceptions, as returned by
|
|
|
|
* {@link #getSuppressedExceptions()}.
|
|
|
|
*
|
|
|
|
* @serial
|
|
|
|
* @since 1.7
|
|
|
|
*/
|
2010-08-12 16:36:49 -07:00
|
|
|
private List<Throwable> suppressedExceptions = null;
|
|
|
|
/*
|
|
|
|
* This field is lazily initialized when the first suppressed
|
|
|
|
* exception is added.
|
|
|
|
*
|
|
|
|
* OutOfMemoryError is preallocated in the VM for better OOM
|
|
|
|
* diagnosability during VM initialization. Constructor can't
|
|
|
|
* be not invoked. If a new field to be added in the future must
|
|
|
|
* be initialized to non-null, it requires a synchronized VM change.
|
|
|
|
*/
|
2010-06-23 17:03:40 -07:00
|
|
|
|
|
|
|
/** Message for trying to suppress a null exception. */
|
|
|
|
private static final String NULL_CAUSE_MESSAGE = "Cannot suppress a null exception.";
|
|
|
|
|
|
|
|
/** Caption for labeling causative exception stack traces */
|
|
|
|
private static final String CAUSE_CAPTION = "Caused by: ";
|
|
|
|
|
|
|
|
/** Caption for labeling suppressed exception stack traces */
|
|
|
|
private static final String SUPPRESSED_CAPTION = "Suppressed: ";
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
/**
|
2010-07-15 18:02:05 -07:00
|
|
|
* Constructs a new throwable with {@code null} as its detail message.
|
2007-12-01 00:00:00 +00:00
|
|
|
* The cause is not initialized, and may subsequently be initialized by a
|
|
|
|
* call to {@link #initCause}.
|
|
|
|
*
|
|
|
|
* <p>The {@link #fillInStackTrace()} method is called to initialize
|
|
|
|
* the stack trace data in the newly created throwable.
|
|
|
|
*/
|
|
|
|
public Throwable() {
|
|
|
|
fillInStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new throwable with the specified detail message. The
|
|
|
|
* cause is not initialized, and may subsequently be initialized by
|
|
|
|
* a call to {@link #initCause}.
|
|
|
|
*
|
|
|
|
* <p>The {@link #fillInStackTrace()} method is called to initialize
|
|
|
|
* the stack trace data in the newly created throwable.
|
|
|
|
*
|
|
|
|
* @param message the detail message. The detail message is saved for
|
|
|
|
* later retrieval by the {@link #getMessage()} method.
|
|
|
|
*/
|
|
|
|
public Throwable(String message) {
|
|
|
|
fillInStackTrace();
|
|
|
|
detailMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new throwable with the specified detail message and
|
|
|
|
* cause. <p>Note that the detail message associated with
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code cause} is <i>not</i> automatically incorporated in
|
2007-12-01 00:00:00 +00:00
|
|
|
* this throwable's detail message.
|
|
|
|
*
|
|
|
|
* <p>The {@link #fillInStackTrace()} method is called to initialize
|
|
|
|
* the stack trace data in the newly created throwable.
|
|
|
|
*
|
|
|
|
* @param message the detail message (which is saved for later retrieval
|
|
|
|
* by the {@link #getMessage()} method).
|
|
|
|
* @param cause the cause (which is saved for later retrieval by the
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@link #getCause()} method). (A {@code null} value is
|
2007-12-01 00:00:00 +00:00
|
|
|
* permitted, and indicates that the cause is nonexistent or
|
|
|
|
* unknown.)
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public Throwable(String message, Throwable cause) {
|
|
|
|
fillInStackTrace();
|
|
|
|
detailMessage = message;
|
|
|
|
this.cause = cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new throwable with the specified cause and a detail
|
2010-07-15 18:02:05 -07:00
|
|
|
* message of {@code (cause==null ? null : cause.toString())} (which
|
|
|
|
* typically contains the class and detail message of {@code cause}).
|
2007-12-01 00:00:00 +00:00
|
|
|
* This constructor is useful for throwables that are little more than
|
|
|
|
* wrappers for other throwables (for example, {@link
|
|
|
|
* java.security.PrivilegedActionException}).
|
|
|
|
*
|
|
|
|
* <p>The {@link #fillInStackTrace()} method is called to initialize
|
|
|
|
* the stack trace data in the newly created throwable.
|
|
|
|
*
|
|
|
|
* @param cause the cause (which is saved for later retrieval by the
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@link #getCause()} method). (A {@code null} value is
|
2007-12-01 00:00:00 +00:00
|
|
|
* permitted, and indicates that the cause is nonexistent or
|
|
|
|
* unknown.)
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public Throwable(Throwable cause) {
|
|
|
|
fillInStackTrace();
|
|
|
|
detailMessage = (cause==null ? null : cause.toString());
|
|
|
|
this.cause = cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the detail message string of this throwable.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* @return the detail message string of this {@code Throwable} instance
|
|
|
|
* (which may be {@code null}).
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public String getMessage() {
|
|
|
|
return detailMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a localized description of this throwable.
|
|
|
|
* Subclasses may override this method in order to produce a
|
|
|
|
* locale-specific message. For subclasses that do not override this
|
|
|
|
* method, the default implementation returns the same result as
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code getMessage()}.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* @return The localized description of this throwable.
|
|
|
|
* @since JDK1.1
|
|
|
|
*/
|
|
|
|
public String getLocalizedMessage() {
|
|
|
|
return getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-15 18:02:05 -07:00
|
|
|
* Returns the cause of this throwable or {@code null} if the
|
2007-12-01 00:00:00 +00:00
|
|
|
* cause is nonexistent or unknown. (The cause is the throwable that
|
|
|
|
* caused this throwable to get thrown.)
|
|
|
|
*
|
|
|
|
* <p>This implementation returns the cause that was supplied via one of
|
2010-07-15 18:02:05 -07:00
|
|
|
* the constructors requiring a {@code Throwable}, or that was set after
|
2007-12-01 00:00:00 +00:00
|
|
|
* creation with the {@link #initCause(Throwable)} method. While it is
|
|
|
|
* typically unnecessary to override this method, a subclass can override
|
|
|
|
* it to return a cause set by some other means. This is appropriate for
|
|
|
|
* a "legacy chained throwable" that predates the addition of chained
|
2010-07-15 18:02:05 -07:00
|
|
|
* exceptions to {@code Throwable}. Note that it is <i>not</i>
|
|
|
|
* necessary to override any of the {@code PrintStackTrace} methods,
|
|
|
|
* all of which invoke the {@code getCause} method to determine the
|
2007-12-01 00:00:00 +00:00
|
|
|
* cause of a throwable.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* @return the cause of this throwable or {@code null} if the
|
2007-12-01 00:00:00 +00:00
|
|
|
* cause is nonexistent or unknown.
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
2010-08-12 16:36:49 -07:00
|
|
|
public synchronized Throwable getCause() {
|
2007-12-01 00:00:00 +00:00
|
|
|
return (cause==this ? null : cause);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the <i>cause</i> of this throwable to the specified value.
|
|
|
|
* (The cause is the throwable that caused this throwable to get thrown.)
|
|
|
|
*
|
|
|
|
* <p>This method can be called at most once. It is generally called from
|
|
|
|
* within the constructor, or immediately after creating the
|
|
|
|
* throwable. If this throwable was created
|
|
|
|
* with {@link #Throwable(Throwable)} or
|
|
|
|
* {@link #Throwable(String,Throwable)}, this method cannot be called
|
|
|
|
* even once.
|
|
|
|
*
|
|
|
|
* @param cause the cause (which is saved for later retrieval by the
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@link #getCause()} method). (A {@code null} value is
|
2007-12-01 00:00:00 +00:00
|
|
|
* permitted, and indicates that the cause is nonexistent or
|
|
|
|
* unknown.)
|
2010-07-15 18:02:05 -07:00
|
|
|
* @return a reference to this {@code Throwable} instance.
|
|
|
|
* @throws IllegalArgumentException if {@code cause} is this
|
2007-12-01 00:00:00 +00:00
|
|
|
* throwable. (A throwable cannot be its own cause.)
|
|
|
|
* @throws IllegalStateException if this throwable was
|
|
|
|
* created with {@link #Throwable(Throwable)} or
|
|
|
|
* {@link #Throwable(String,Throwable)}, or this method has already
|
|
|
|
* been called on this throwable.
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public synchronized Throwable initCause(Throwable cause) {
|
|
|
|
if (this.cause != this)
|
|
|
|
throw new IllegalStateException("Can't overwrite cause");
|
|
|
|
if (cause == this)
|
|
|
|
throw new IllegalArgumentException("Self-causation not permitted");
|
|
|
|
this.cause = cause;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a short description of this throwable.
|
|
|
|
* The result is the concatenation of:
|
|
|
|
* <ul>
|
|
|
|
* <li> the {@linkplain Class#getName() name} of the class of this object
|
|
|
|
* <li> ": " (a colon and a space)
|
|
|
|
* <li> the result of invoking this object's {@link #getLocalizedMessage}
|
|
|
|
* method
|
|
|
|
* </ul>
|
2010-07-15 18:02:05 -07:00
|
|
|
* If {@code getLocalizedMessage} returns {@code null}, then just
|
2007-12-01 00:00:00 +00:00
|
|
|
* the class name is returned.
|
|
|
|
*
|
|
|
|
* @return a string representation of this throwable.
|
|
|
|
*/
|
|
|
|
public String toString() {
|
|
|
|
String s = getClass().getName();
|
|
|
|
String message = getLocalizedMessage();
|
|
|
|
return (message != null) ? (s + ": " + message) : s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints this throwable and its backtrace to the
|
|
|
|
* standard error stream. This method prints a stack trace for this
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code Throwable} object on the error output stream that is
|
|
|
|
* the value of the field {@code System.err}. The first line of
|
2007-12-01 00:00:00 +00:00
|
|
|
* output contains the result of the {@link #toString()} method for
|
|
|
|
* this object. Remaining lines represent data previously recorded by
|
|
|
|
* the method {@link #fillInStackTrace()}. The format of this
|
|
|
|
* information depends on the implementation, but the following
|
|
|
|
* example may be regarded as typical:
|
|
|
|
* <blockquote><pre>
|
|
|
|
* java.lang.NullPointerException
|
|
|
|
* at MyClass.mash(MyClass.java:9)
|
|
|
|
* at MyClass.crunch(MyClass.java:6)
|
|
|
|
* at MyClass.main(MyClass.java:3)
|
|
|
|
* </pre></blockquote>
|
|
|
|
* This example was produced by running the program:
|
|
|
|
* <pre>
|
|
|
|
* class MyClass {
|
|
|
|
* public static void main(String[] args) {
|
|
|
|
* crunch(null);
|
|
|
|
* }
|
|
|
|
* static void crunch(int[] a) {
|
|
|
|
* mash(a);
|
|
|
|
* }
|
|
|
|
* static void mash(int[] b) {
|
|
|
|
* System.out.println(b[0]);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
* The backtrace for a throwable with an initialized, non-null cause
|
|
|
|
* should generally include the backtrace for the cause. The format
|
|
|
|
* of this information depends on the implementation, but the following
|
|
|
|
* example may be regarded as typical:
|
|
|
|
* <pre>
|
|
|
|
* HighLevelException: MidLevelException: LowLevelException
|
|
|
|
* at Junk.a(Junk.java:13)
|
|
|
|
* at Junk.main(Junk.java:4)
|
|
|
|
* Caused by: MidLevelException: LowLevelException
|
|
|
|
* at Junk.c(Junk.java:23)
|
|
|
|
* at Junk.b(Junk.java:17)
|
|
|
|
* at Junk.a(Junk.java:11)
|
|
|
|
* ... 1 more
|
|
|
|
* Caused by: LowLevelException
|
|
|
|
* at Junk.e(Junk.java:30)
|
|
|
|
* at Junk.d(Junk.java:27)
|
|
|
|
* at Junk.c(Junk.java:21)
|
|
|
|
* ... 3 more
|
|
|
|
* </pre>
|
2010-07-15 18:02:05 -07:00
|
|
|
* Note the presence of lines containing the characters {@code "..."}.
|
2007-12-01 00:00:00 +00:00
|
|
|
* These lines indicate that the remainder of the stack trace for this
|
|
|
|
* exception matches the indicated number of frames from the bottom of the
|
|
|
|
* stack trace of the exception that was caused by this exception (the
|
|
|
|
* "enclosing" exception). This shorthand can greatly reduce the length
|
|
|
|
* of the output in the common case where a wrapped exception is thrown
|
|
|
|
* from same method as the "causative exception" is caught. The above
|
|
|
|
* example was produced by running the program:
|
|
|
|
* <pre>
|
|
|
|
* public class Junk {
|
|
|
|
* public static void main(String args[]) {
|
|
|
|
* try {
|
|
|
|
* a();
|
|
|
|
* } catch(HighLevelException e) {
|
|
|
|
* e.printStackTrace();
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* static void a() throws HighLevelException {
|
|
|
|
* try {
|
|
|
|
* b();
|
|
|
|
* } catch(MidLevelException e) {
|
|
|
|
* throw new HighLevelException(e);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* static void b() throws MidLevelException {
|
|
|
|
* c();
|
|
|
|
* }
|
|
|
|
* static void c() throws MidLevelException {
|
|
|
|
* try {
|
|
|
|
* d();
|
|
|
|
* } catch(LowLevelException e) {
|
|
|
|
* throw new MidLevelException(e);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* static void d() throws LowLevelException {
|
|
|
|
* e();
|
|
|
|
* }
|
|
|
|
* static void e() throws LowLevelException {
|
|
|
|
* throw new LowLevelException();
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class HighLevelException extends Exception {
|
|
|
|
* HighLevelException(Throwable cause) { super(cause); }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class MidLevelException extends Exception {
|
|
|
|
* MidLevelException(Throwable cause) { super(cause); }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class LowLevelException extends Exception {
|
|
|
|
* }
|
|
|
|
* </pre>
|
2010-06-23 17:03:40 -07:00
|
|
|
* As of release 7, the platform supports the notion of
|
2010-08-25 15:35:45 -07:00
|
|
|
* <i>suppressed exceptions</i> (in conjunction with the {@code
|
|
|
|
* try}-with-resources statement). Any exceptions that were
|
2010-06-23 17:03:40 -07:00
|
|
|
* suppressed in order to deliver an exception are printed out
|
|
|
|
* beneath the stack trace. The format of this information
|
|
|
|
* depends on the implementation, but the following example may be
|
|
|
|
* regarded as typical:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* Exception in thread "main" java.lang.Exception: Something happened
|
|
|
|
* at Foo.bar(Foo.java:10)
|
|
|
|
* at Foo.main(Foo.java:5)
|
|
|
|
* Suppressed: Resource$CloseFailException: Resource ID = 0
|
|
|
|
* at Resource.close(Resource.java:26)
|
|
|
|
* at Foo.bar(Foo.java:9)
|
|
|
|
* ... 1 more
|
|
|
|
* </pre>
|
|
|
|
* Note that the "... n more" notation is used on suppressed exceptions
|
|
|
|
* just at it is used on causes. Unlike causes, suppressed exceptions are
|
|
|
|
* indented beyond their "containing exceptions."
|
|
|
|
*
|
|
|
|
* <p>An exception can have both a cause and one or more suppressed
|
|
|
|
* exceptions:
|
|
|
|
* <pre>
|
|
|
|
* Exception in thread "main" java.lang.Exception: Main block
|
|
|
|
* at Foo3.main(Foo3.java:7)
|
|
|
|
* Suppressed: Resource$CloseFailException: Resource ID = 2
|
|
|
|
* at Resource.close(Resource.java:26)
|
|
|
|
* at Foo3.main(Foo3.java:5)
|
|
|
|
* Suppressed: Resource$CloseFailException: Resource ID = 1
|
|
|
|
* at Resource.close(Resource.java:26)
|
|
|
|
* at Foo3.main(Foo3.java:5)
|
|
|
|
* Caused by: java.lang.Exception: I did it
|
|
|
|
* at Foo3.main(Foo3.java:8)
|
|
|
|
* </pre>
|
|
|
|
* Likewise, a suppressed exception can have a cause:
|
|
|
|
* <pre>
|
|
|
|
* Exception in thread "main" java.lang.Exception: Main block
|
|
|
|
* at Foo4.main(Foo4.java:6)
|
|
|
|
* Suppressed: Resource2$CloseFailException: Resource ID = 1
|
|
|
|
* at Resource2.close(Resource2.java:20)
|
|
|
|
* at Foo4.main(Foo4.java:5)
|
|
|
|
* Caused by: java.lang.Exception: Rats, you caught me
|
|
|
|
* at Resource2$CloseFailException.<init>(Resource2.java:45)
|
|
|
|
* ... 2 more
|
|
|
|
* </pre>
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public void printStackTrace() {
|
|
|
|
printStackTrace(System.err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints this throwable and its backtrace to the specified print stream.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* @param s {@code PrintStream} to use for output
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public void printStackTrace(PrintStream s) {
|
2010-06-23 17:03:40 -07:00
|
|
|
printStackTrace(new WrappedPrintStream(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void printStackTrace(PrintStreamOrWriter s) {
|
2010-07-15 18:02:05 -07:00
|
|
|
// Guard against malicious overrides of Throwable.equals by
|
|
|
|
// using a Set with identity equality semantics.
|
|
|
|
Set<Throwable> dejaVu =
|
|
|
|
Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
|
2010-06-23 17:03:40 -07:00
|
|
|
dejaVu.add(this);
|
|
|
|
|
|
|
|
synchronized (s.lock()) {
|
|
|
|
// Print our stack trace
|
2007-12-01 00:00:00 +00:00
|
|
|
s.println(this);
|
|
|
|
StackTraceElement[] trace = getOurStackTrace();
|
2010-06-23 17:03:40 -07:00
|
|
|
for (StackTraceElement traceElement : trace)
|
|
|
|
s.println("\tat " + traceElement);
|
|
|
|
|
|
|
|
// Print suppressed exceptions, if any
|
2010-08-12 16:36:49 -07:00
|
|
|
for (Throwable se : getSuppressedExceptions())
|
2010-06-23 17:03:40 -07:00
|
|
|
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-06-23 17:03:40 -07:00
|
|
|
// Print cause, if any
|
2007-12-01 00:00:00 +00:00
|
|
|
Throwable ourCause = getCause();
|
|
|
|
if (ourCause != null)
|
2010-06-23 17:03:40 -07:00
|
|
|
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-23 17:03:40 -07:00
|
|
|
* Print our stack trace as an enclosed exception for the specified
|
|
|
|
* stack trace.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
2010-06-23 17:03:40 -07:00
|
|
|
private void printEnclosedStackTrace(PrintStreamOrWriter s,
|
|
|
|
StackTraceElement[] enclosingTrace,
|
|
|
|
String caption,
|
|
|
|
String prefix,
|
|
|
|
Set<Throwable> dejaVu) {
|
|
|
|
assert Thread.holdsLock(s.lock());
|
|
|
|
if (dejaVu.contains(this)) {
|
|
|
|
s.println("\t[CIRCULAR REFERENCE:" + this + "]");
|
|
|
|
} else {
|
|
|
|
dejaVu.add(this);
|
|
|
|
// Compute number of frames in common between this and enclosing trace
|
|
|
|
StackTraceElement[] trace = getOurStackTrace();
|
|
|
|
int m = trace.length - 1;
|
|
|
|
int n = enclosingTrace.length - 1;
|
|
|
|
while (m >= 0 && n >=0 && trace[m].equals(enclosingTrace[n])) {
|
|
|
|
m--; n--;
|
|
|
|
}
|
|
|
|
int framesInCommon = trace.length - 1 - m;
|
|
|
|
|
|
|
|
// Print our stack trace
|
|
|
|
s.println(prefix + caption + this);
|
|
|
|
for (int i = 0; i <= m; i++)
|
|
|
|
s.println(prefix + "\tat " + trace[i]);
|
|
|
|
if (framesInCommon != 0)
|
|
|
|
s.println(prefix + "\t... " + framesInCommon + " more");
|
|
|
|
|
|
|
|
// Print suppressed exceptions, if any
|
2010-08-12 16:36:49 -07:00
|
|
|
for (Throwable se : getSuppressedExceptions())
|
2010-06-23 17:03:40 -07:00
|
|
|
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION,
|
|
|
|
prefix +"\t", dejaVu);
|
|
|
|
|
|
|
|
// Print cause, if any
|
|
|
|
Throwable ourCause = getCause();
|
|
|
|
if (ourCause != null)
|
|
|
|
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints this throwable and its backtrace to the specified
|
|
|
|
* print writer.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* @param s {@code PrintWriter} to use for output
|
2007-12-01 00:00:00 +00:00
|
|
|
* @since JDK1.1
|
|
|
|
*/
|
|
|
|
public void printStackTrace(PrintWriter s) {
|
2010-06-23 17:03:40 -07:00
|
|
|
printStackTrace(new WrappedPrintWriter(s));
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-23 17:03:40 -07:00
|
|
|
* Wrapper class for PrintStream and PrintWriter to enable a single
|
|
|
|
* implementation of printStackTrace.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
2010-06-23 17:03:40 -07:00
|
|
|
private abstract static class PrintStreamOrWriter {
|
|
|
|
/** Returns the object to be locked when using this StreamOrWriter */
|
|
|
|
abstract Object lock();
|
|
|
|
|
|
|
|
/** Prints the specified string as a line on this StreamOrWriter */
|
|
|
|
abstract void println(Object o);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class WrappedPrintStream extends PrintStreamOrWriter {
|
|
|
|
private final PrintStream printStream;
|
|
|
|
|
|
|
|
WrappedPrintStream(PrintStream printStream) {
|
|
|
|
this.printStream = printStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object lock() {
|
|
|
|
return printStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void println(Object o) {
|
|
|
|
printStream.println(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class WrappedPrintWriter extends PrintStreamOrWriter {
|
|
|
|
private final PrintWriter printWriter;
|
|
|
|
|
|
|
|
WrappedPrintWriter(PrintWriter printWriter) {
|
|
|
|
this.printWriter = printWriter;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object lock() {
|
|
|
|
return printWriter;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-06-23 17:03:40 -07:00
|
|
|
void println(Object o) {
|
|
|
|
printWriter.println(o);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fills in the execution stack trace. This method records within this
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code Throwable} object information about the current state of
|
2007-12-01 00:00:00 +00:00
|
|
|
* the stack frames for the current thread.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* @return a reference to this {@code Throwable} instance.
|
2007-12-01 00:00:00 +00:00
|
|
|
* @see java.lang.Throwable#printStackTrace()
|
|
|
|
*/
|
|
|
|
public synchronized native Throwable fillInStackTrace();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides programmatic access to the stack trace information printed by
|
|
|
|
* {@link #printStackTrace()}. Returns an array of stack trace elements,
|
|
|
|
* each representing one stack frame. The zeroth element of the array
|
|
|
|
* (assuming the array's length is non-zero) represents the top of the
|
|
|
|
* stack, which is the last method invocation in the sequence. Typically,
|
|
|
|
* this is the point at which this throwable was created and thrown.
|
|
|
|
* The last element of the array (assuming the array's length is non-zero)
|
|
|
|
* represents the bottom of the stack, which is the first method invocation
|
|
|
|
* in the sequence.
|
|
|
|
*
|
|
|
|
* <p>Some virtual machines may, under some circumstances, omit one
|
|
|
|
* or more stack frames from the stack trace. In the extreme case,
|
|
|
|
* a virtual machine that has no stack trace information concerning
|
|
|
|
* this throwable is permitted to return a zero-length array from this
|
|
|
|
* method. Generally speaking, the array returned by this method will
|
|
|
|
* contain one element for every frame that would be printed by
|
2010-07-15 18:02:05 -07:00
|
|
|
* {@code printStackTrace}.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* @return an array of stack trace elements representing the stack trace
|
|
|
|
* pertaining to this throwable.
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public StackTraceElement[] getStackTrace() {
|
|
|
|
return getOurStackTrace().clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized StackTraceElement[] getOurStackTrace() {
|
|
|
|
// Initialize stack trace if this is the first call to this method
|
|
|
|
if (stackTrace == null) {
|
|
|
|
int depth = getStackTraceDepth();
|
|
|
|
stackTrace = new StackTraceElement[depth];
|
|
|
|
for (int i=0; i < depth; i++)
|
|
|
|
stackTrace[i] = getStackTraceElement(i);
|
|
|
|
}
|
|
|
|
return stackTrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the stack trace elements that will be returned by
|
|
|
|
* {@link #getStackTrace()} and printed by {@link #printStackTrace()}
|
|
|
|
* and related methods.
|
|
|
|
*
|
|
|
|
* This method, which is designed for use by RPC frameworks and other
|
|
|
|
* advanced systems, allows the client to override the default
|
|
|
|
* stack trace that is either generated by {@link #fillInStackTrace()}
|
|
|
|
* when a throwable is constructed or deserialized when a throwable is
|
|
|
|
* read from a serialization stream.
|
|
|
|
*
|
|
|
|
* @param stackTrace the stack trace elements to be associated with
|
2010-07-15 18:02:05 -07:00
|
|
|
* this {@code Throwable}. The specified array is copied by this
|
2007-12-01 00:00:00 +00:00
|
|
|
* call; changes in the specified array after the method invocation
|
2010-07-15 18:02:05 -07:00
|
|
|
* returns will have no affect on this {@code Throwable}'s stack
|
2007-12-01 00:00:00 +00:00
|
|
|
* trace.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* @throws NullPointerException if {@code stackTrace} is
|
|
|
|
* {@code null}, or if any of the elements of
|
|
|
|
* {@code stackTrace} are {@code null}
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public void setStackTrace(StackTraceElement[] stackTrace) {
|
|
|
|
StackTraceElement[] defensiveCopy = stackTrace.clone();
|
|
|
|
for (int i = 0; i < defensiveCopy.length; i++)
|
|
|
|
if (defensiveCopy[i] == null)
|
|
|
|
throw new NullPointerException("stackTrace[" + i + "]");
|
|
|
|
|
2010-08-12 16:36:49 -07:00
|
|
|
synchronized (this) {
|
|
|
|
this.stackTrace = defensiveCopy;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of elements in the stack trace (or 0 if the stack
|
|
|
|
* trace is unavailable).
|
2009-06-14 14:33:30 -07:00
|
|
|
*
|
|
|
|
* package-protection for use by SharedSecrets.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
2009-06-14 14:33:30 -07:00
|
|
|
native int getStackTraceDepth();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the specified element of the stack trace.
|
|
|
|
*
|
2009-06-14 14:33:30 -07:00
|
|
|
* package-protection for use by SharedSecrets.
|
|
|
|
*
|
2007-12-01 00:00:00 +00:00
|
|
|
* @param index index of the element to return.
|
2010-07-15 18:02:05 -07:00
|
|
|
* @throws IndexOutOfBoundsException if {@code index < 0 ||
|
|
|
|
* index >= getStackTraceDepth() }
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
2009-06-14 14:33:30 -07:00
|
|
|
native StackTraceElement getStackTraceElement(int index);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-06-23 17:03:40 -07:00
|
|
|
private void readObject(ObjectInputStream s)
|
|
|
|
throws IOException, ClassNotFoundException {
|
|
|
|
s.defaultReadObject(); // read in all fields
|
2010-08-12 16:36:49 -07:00
|
|
|
List<Throwable> suppressed = null;
|
2010-06-23 17:03:40 -07:00
|
|
|
if (suppressedExceptions != null &&
|
|
|
|
!suppressedExceptions.isEmpty()) { // Copy Throwables to new list
|
|
|
|
suppressed = new ArrayList<Throwable>();
|
2010-08-12 16:36:49 -07:00
|
|
|
for (Throwable t : suppressedExceptions) {
|
2010-06-23 17:03:40 -07:00
|
|
|
if (t == null)
|
|
|
|
throw new NullPointerException(NULL_CAUSE_MESSAGE);
|
|
|
|
suppressed.add(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
suppressedExceptions = suppressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void writeObject(ObjectOutputStream s)
|
2007-12-01 00:00:00 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
getOurStackTrace(); // Ensure that stackTrace field is initialized.
|
|
|
|
s.defaultWriteObject();
|
|
|
|
}
|
2010-06-23 17:03:40 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the specified exception to the list of exceptions that
|
2010-08-25 15:35:45 -07:00
|
|
|
* were suppressed, typically by the {@code try}-with-resources
|
2010-06-23 17:03:40 -07:00
|
|
|
* statement, in order to deliver this exception.
|
|
|
|
*
|
2010-07-15 18:02:05 -07:00
|
|
|
* <p>Note that when one exception {@linkplain
|
|
|
|
* #initCause(Throwable) causes} another exception, the first
|
|
|
|
* exception is usually caught and then the second exception is
|
|
|
|
* thrown in response. In contrast, when one exception suppresses
|
|
|
|
* another, two exceptions are thrown in sibling code blocks, such
|
|
|
|
* as in a {@code try} block and in its {@code finally} block, and
|
|
|
|
* control flow can only continue with one exception so the second
|
|
|
|
* is recorded as a suppressed exception of the first.
|
|
|
|
*
|
2010-06-23 17:03:40 -07:00
|
|
|
* @param exception the exception to be added to the list of
|
|
|
|
* suppressed exceptions
|
|
|
|
* @throws NullPointerException if {@code exception} is null
|
2010-07-15 18:02:05 -07:00
|
|
|
* @throws IllegalArgumentException if {@code exception} is this
|
|
|
|
* throwable; a throwable cannot suppress itself.
|
2010-06-23 17:03:40 -07:00
|
|
|
* @since 1.7
|
|
|
|
*/
|
|
|
|
public synchronized void addSuppressedException(Throwable exception) {
|
|
|
|
if (exception == null)
|
|
|
|
throw new NullPointerException(NULL_CAUSE_MESSAGE);
|
2010-07-15 18:02:05 -07:00
|
|
|
if (exception == this)
|
|
|
|
throw new IllegalArgumentException("Self-suppression not permitted");
|
2010-06-23 17:03:40 -07:00
|
|
|
|
2010-08-12 16:36:49 -07:00
|
|
|
if (suppressedExceptions == null)
|
2010-06-23 17:03:40 -07:00
|
|
|
suppressedExceptions = new ArrayList<Throwable>();
|
|
|
|
suppressedExceptions.add(exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Throwable[] EMPTY_THROWABLE_ARRAY = new Throwable[0];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array containing all of the exceptions that were
|
2010-08-25 15:35:45 -07:00
|
|
|
* suppressed, typically by the {@code try}-with-resources
|
2010-06-23 17:03:40 -07:00
|
|
|
* statement, in order to deliver this exception.
|
|
|
|
*
|
|
|
|
* @return an array containing all of the exceptions that were
|
|
|
|
* suppressed to deliver this exception.
|
|
|
|
* @since 1.7
|
|
|
|
*/
|
2010-08-12 16:36:49 -07:00
|
|
|
public synchronized Throwable[] getSuppressedExceptions() {
|
|
|
|
if (suppressedExceptions == null)
|
|
|
|
return EMPTY_THROWABLE_ARRAY;
|
|
|
|
else
|
|
|
|
return suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY);
|
2010-06-23 17:03:40 -07:00
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|