⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 throwable.java

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)Throwable.java	1.53 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program 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 at /legal/license.txt).    *    * 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    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package java.lang;import  java.io.*;/** * The <code>Throwable</code> class is the superclass of all errors and * 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 * can be thrown by the Java <code>throw</code> statement. Similarly, only * this class or one of its subclasses can be the argument type in a * <code>catch</code> clause. *  * <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 * <tt>java.io</tt>.  Suppose the internals of the <tt>put</tt> method  * can throw an {@link java.io.IOException IOException}.  The implementation * can communicate the details of the <tt>IOException</tt> to its caller * while conforming to the <tt>Collection</tt> interface by wrapping the * <tt>IOException</tt> in an appropriate unchecked exception.  (The * 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 * <tt>Throwable</tt> constructors that takes a cause.  For example: * <pre> *     try { *         lowLevelOp(); *     } catch (LowLevelException le) { *         throw new HighLevelException(le);  // Chaining-aware constructor *     } * </pre> * Because the <tt>initCause</tt> method is public, it allows a cause to be * associated with any throwable, even a "legacy throwable" whose * implementation predates the addition of the exception chaining mechanism to * <tt>Throwable</tt>. For example: * <pre> *     try { *         lowLevelOp(); *     } catch (LowLevelException le) { *         throw (HighLevelException)                 new HighLevelException().initCause(le);  // Legacy constructor *     } * </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} and * {@link java.rmi.RemoteException}). * As of release 1.4, all of these throwables have been retrofitted to  * use the standard exception chaining mechanism, while continuing to * implement their "legacy" chaining mechanisms for compatibility. * NOTE: <B>java.rmi.RemoteException</B> is found in J2ME CDC optional * packages such as J2ME RMI Optional Package. * * <p>Further, as of release 1.4, many general purpose <tt>Throwable</tt> * 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 * <tt>initCause</tt> method, but it is more convenient and expressive to * delegate to a constructor that takes a cause. *  * <p>By convention, class <code>Throwable</code> and its subclasses have two * constructors, one that takes no arguments and one that takes a * <code>String</code> argument that can be used to produce a detail message. * Further, those subclasses that might likely have a cause associated with * them should have two more constructors, one that takes a * <code>Throwable</code> (the cause), and one that takes a * <code>String</code> (the detail message) and a <code>Throwable</code> (the * 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 * <i>serialized representation</i> of this class so <tt>getStackTrace</tt> * and <tt>printStackTrace</tt> will operate properly on a throwable that * was obtained by deserialization. * * @author  unascribed * @author  Josh Bloch (Added exception chaining and programmatic access to *          stack trace in 1.4.) * @version 1.53, 10/10/06 * @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     * <tt>FileNotFoundException</tt>, this contains the name of     * 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.     */    /**     * Constructs a new throwable with <code>null</code> as its 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.     */    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     * <code>cause</code> is <i>not</i> automatically incorporated in     * 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     *         {@link #getCause()} method).  (A <tt>null</tt> value is     *         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     * message of <tt>(cause==null ? null : cause.toString())</tt> (which     * typically contains the class and detail message of <tt>cause</tt>).     * 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     *         {@link #getCause()} method).  (A <tt>null</tt> value is     *         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.     *     * @return  the detail message string of this <tt>Throwable</tt> instance     *          (which may be <tt>null</tt>).     */    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     * <code>getMessage()</code>.     *     * @return  The localized description of this throwable.     * @since   JDK1.1     */    public String getLocalizedMessage() {        return getMessage();    }    /**     * Returns the cause of this throwable or <code>null</code> if the     * 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     * the constructors requiring a <tt>Throwable</tt>, or that was set after     * 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     * exceptions to <tt>Throwable</tt>.  Note that it is <i>not</i>     * necessary to override any of the <tt>PrintStackTrace</tt> methods,     * all of which invoke the <tt>getCause</tt> method to determine the     * cause of a throwable.     *     * @return  the cause of this throwable or <code>null</code> if the     *          cause is nonexistent or unknown.     * @since 1.4     */    public Throwable getCause() {        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     *         {@link #getCause()} method).  (A <tt>null</tt> value is     *         permitted, and indicates that the cause is nonexistent or     *         unknown.)     * @return  a reference to this <code>Throwable</code> instance.     * @throws IllegalArgumentException if <code>cause</code> is this     *         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)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -