src/share/classes/java/lang/Throwable.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 792     /**
 793      * Returns the number of elements in the stack trace (or 0 if the stack
 794      * trace is unavailable).
 795      *
 796      * package-protection for use by SharedSecrets.
 797      */
 798     native int getStackTraceDepth();
 799 
 800     /**
 801      * Returns the specified element of the stack trace.
 802      *
 803      * package-protection for use by SharedSecrets.
 804      *
 805      * @param index index of the element to return.
 806      * @throws IndexOutOfBoundsException if {@code index < 0 ||
 807      *         index >= getStackTraceDepth() }
 808      */
 809     native StackTraceElement getStackTraceElement(int index);
 810 
 811     /**
 812      * Read a {@code Throwable} from a stream, enforcing
 813      * well-formedness constraints on fields.  Null entries and
 814      * self-pointers are not allowed in the list of {@code
 815      * suppressedExceptions}.  Null entries are not allowed for stack
 816      * trace elements.
 817      *
 818      * Note that there are no constraints on the value the {@code
 819      * cause} field can hold; both {@code null} and {@code this} are
 820      * valid values for the field.
 821      */
 822     private void readObject(ObjectInputStream s)
 823         throws IOException, ClassNotFoundException {
 824         s.defaultReadObject();     // read in all fields
 825         if (suppressedExceptions != null) {
 826             List<Throwable> suppressed = null;
 827             if (suppressedExceptions.isEmpty()) {
 828                 // Use the sentinel for a zero-length list
 829                 suppressed = SUPPRESSED_SENTINEL;
 830             } else { // Copy Throwables to new list
 831                 suppressed = new ArrayList<>(1);
 832                 for (Throwable t : suppressedExceptions) {


 848                     throw new NullPointerException("null StackTraceElement in serial stream. ");
 849             }
 850         } else {
 851             // A null stackTrace field in the serial form can result from
 852             // an exception serialized without that field in older JDK releases.
 853             stackTrace = EMPTY_STACK;
 854         }
 855 
 856     }
 857 
 858     /**
 859      * Write a {@code Throwable} object to a stream.
 860      */
 861     private synchronized void writeObject(ObjectOutputStream s)
 862         throws IOException {
 863         getOurStackTrace();  // Ensure that stackTrace field is initialized.
 864         s.defaultWriteObject();
 865     }
 866 
 867     /**
 868      * Adds the specified exception to the list of exceptions that
 869      * were suppressed, typically by the {@code try}-with-resources
 870      * statement, in order to deliver this exception.

 871      *
 872      * If the first exception to be suppressed is {@code null}, that
 873      * indicates suppressed exception information will <em>not</em> be
 874      * recorded for this exception.  Subsequent calls to this method
 875      * will not record any suppressed exceptions.  Otherwise,
 876      * attempting to suppress {@code null} after an exception has
 877      * already been successfully suppressed results in a {@code
 878      * NullPointerException}.
 879      *
 880      * <p>Note that when one exception {@linkplain
 881      * #initCause(Throwable) causes} another exception, the first
 882      * exception is usually caught and then the second exception is
 883      * thrown in response.  In contrast, when one exception suppresses
 884      * another, two exceptions are thrown in sibling code blocks, such
 885      * as in a {@code try} block and in its {@code finally} block, and
 886      * control flow can only continue with one exception so the second
 887      * is recorded as a suppressed exception of the first.
 888      *
 889      * @param exception the exception to be added to the list of
 890      *        suppressed exceptions


   1 /*
   2  * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 792     /**
 793      * Returns the number of elements in the stack trace (or 0 if the stack
 794      * trace is unavailable).
 795      *
 796      * package-protection for use by SharedSecrets.
 797      */
 798     native int getStackTraceDepth();
 799 
 800     /**
 801      * Returns the specified element of the stack trace.
 802      *
 803      * package-protection for use by SharedSecrets.
 804      *
 805      * @param index index of the element to return.
 806      * @throws IndexOutOfBoundsException if {@code index < 0 ||
 807      *         index >= getStackTraceDepth() }
 808      */
 809     native StackTraceElement getStackTraceElement(int index);
 810 
 811     /**
 812      * Reads a {@code Throwable} from a stream, enforcing
 813      * well-formedness constraints on fields.  Null entries and
 814      * self-pointers are not allowed in the list of {@code
 815      * suppressedExceptions}.  Null entries are not allowed for stack
 816      * trace elements.
 817      *
 818      * Note that there are no constraints on the value the {@code
 819      * cause} field can hold; both {@code null} and {@code this} are
 820      * valid values for the field.
 821      */
 822     private void readObject(ObjectInputStream s)
 823         throws IOException, ClassNotFoundException {
 824         s.defaultReadObject();     // read in all fields
 825         if (suppressedExceptions != null) {
 826             List<Throwable> suppressed = null;
 827             if (suppressedExceptions.isEmpty()) {
 828                 // Use the sentinel for a zero-length list
 829                 suppressed = SUPPRESSED_SENTINEL;
 830             } else { // Copy Throwables to new list
 831                 suppressed = new ArrayList<>(1);
 832                 for (Throwable t : suppressedExceptions) {


 848                     throw new NullPointerException("null StackTraceElement in serial stream. ");
 849             }
 850         } else {
 851             // A null stackTrace field in the serial form can result from
 852             // an exception serialized without that field in older JDK releases.
 853             stackTrace = EMPTY_STACK;
 854         }
 855 
 856     }
 857 
 858     /**
 859      * Write a {@code Throwable} object to a stream.
 860      */
 861     private synchronized void writeObject(ObjectOutputStream s)
 862         throws IOException {
 863         getOurStackTrace();  // Ensure that stackTrace field is initialized.
 864         s.defaultWriteObject();
 865     }
 866 
 867     /**
 868      * Appends the specified exception to the exceptions that were
 869      * suppressed in order to deliver this exception. This method is
 870      * typically called (automatically and implicitly) by the {@code
 871      * try}-with-resources statement.
 872      *
 873      * If the first exception to be suppressed is {@code null}, that
 874      * indicates suppressed exception information will <em>not</em> be
 875      * recorded for this exception.  Subsequent calls to this method
 876      * will not record any suppressed exceptions.  Otherwise,
 877      * attempting to suppress {@code null} after an exception has
 878      * already been successfully suppressed results in a {@code
 879      * NullPointerException}.
 880      *
 881      * <p>Note that when one exception {@linkplain
 882      * #initCause(Throwable) causes} another exception, the first
 883      * exception is usually caught and then the second exception is
 884      * thrown in response.  In contrast, when one exception suppresses
 885      * another, two exceptions are thrown in sibling code blocks, such
 886      * as in a {@code try} block and in its {@code finally} block, and
 887      * control flow can only continue with one exception so the second
 888      * is recorded as a suppressed exception of the first.
 889      *
 890      * @param exception the exception to be added to the list of
 891      *        suppressed exceptions