831 * @param stackTrace the stack trace elements to be associated with
832 * this {@code Throwable}. The specified array is copied by this
833 * call; changes in the specified array after the method invocation
834 * returns will have no affect on this {@code Throwable}'s stack
835 * trace.
836 *
837 * @throws NullPointerException if {@code stackTrace} is
838 * {@code null} or if any of the elements of
839 * {@code stackTrace} are {@code null}
840 *
841 * @since 1.4
842 */
843 public void setStackTrace(StackTraceElement[] stackTrace) {
844 // Validate argument
845 StackTraceElement[] defensiveCopy = stackTrace.clone();
846 for (int i = 0; i < defensiveCopy.length; i++) {
847 if (defensiveCopy[i] == null)
848 throw new NullPointerException("stackTrace[" + i + "]");
849 }
850
851 if (this.stackTrace == null) // Immutable stack
852 return;
853
854 synchronized (this) {
855 this.stackTrace = defensiveCopy;
856 }
857 }
858
859 /**
860 * Returns the number of elements in the stack trace (or 0 if the stack
861 * trace is unavailable).
862 *
863 * package-protection for use by SharedSecrets.
864 */
865 native int getStackTraceDepth();
866
867 /**
868 * Returns the specified element of the stack trace.
869 *
870 * package-protection for use by SharedSecrets.
871 *
872 * @param index index of the element to return.
873 * @throws IndexOutOfBoundsException if {@code index < 0 ||
874 * index >= getStackTraceDepth() }
941 // older JDK releases; treat such exceptions as having
942 // empty stack traces.
943 stackTrace = UNASSIGNED_STACK.clone();
944 }
945 }
946
947 /**
948 * Write a {@code Throwable} object to a stream.
949 *
950 * A {@code null} stack trace field is represented in the serial
951 * form as a one-element array whose element is equal to {@code
952 * new StackTraceElement("", "", null, Integer.MIN_VALUE)}.
953 */
954 private synchronized void writeObject(ObjectOutputStream s)
955 throws IOException {
956 // Ensure that the stackTrace field is initialized to a
957 // non-null value, if appropriate. As of JDK 7, a null stack
958 // trace field is a valid value indicating the stack trace
959 // should not be set.
960 getOurStackTrace();
961 ObjectOutputStream.PutField fields = s.putFields();
962
963 fields.put("detailMessage", detailMessage);
964 fields.put("cause", cause);
965 // Serialize a null stacktrace using the stack trace sentinel.
966 if (stackTrace == null)
967 fields.put("stackTrace", SentinelHolder.STACK_TRACE_SENTINEL);
968 else
969 fields.put("stackTrace", stackTrace);
970 fields.put("suppressedExceptions", suppressedExceptions);
971
972 s.writeFields();
973 }
974
975 /**
976 * Appends the specified exception to the exceptions that were
977 * suppressed in order to deliver this exception. This method is
978 * typically called (automatically and implicitly) by the {@code
979 * try}-with-resources statement.
980 *
981 * <p>The suppression behavior is enabled <em>unless</em> disabled
982 * {@linkplain #Throwable(String, Throwable, boolean, boolean) via
983 * a constructor}. When suppression is disabled, this method does
984 * nothing other than to validate its argument.
985 *
986 * <p>Note that when one exception {@linkplain
987 * #initCause(Throwable) causes} another exception, the first
988 * exception is usually caught and then the second exception is
989 * thrown in response. In other words, there is a causal
990 * connection between the two exceptions.
991 *
992 * In contrast, there are situations where two independent
|
831 * @param stackTrace the stack trace elements to be associated with
832 * this {@code Throwable}. The specified array is copied by this
833 * call; changes in the specified array after the method invocation
834 * returns will have no affect on this {@code Throwable}'s stack
835 * trace.
836 *
837 * @throws NullPointerException if {@code stackTrace} is
838 * {@code null} or if any of the elements of
839 * {@code stackTrace} are {@code null}
840 *
841 * @since 1.4
842 */
843 public void setStackTrace(StackTraceElement[] stackTrace) {
844 // Validate argument
845 StackTraceElement[] defensiveCopy = stackTrace.clone();
846 for (int i = 0; i < defensiveCopy.length; i++) {
847 if (defensiveCopy[i] == null)
848 throw new NullPointerException("stackTrace[" + i + "]");
849 }
850
851 synchronized (this) {
852 if (this.stackTrace == null) // Immutable stack
853 return;
854 this.stackTrace = defensiveCopy;
855 }
856 }
857
858 /**
859 * Returns the number of elements in the stack trace (or 0 if the stack
860 * trace is unavailable).
861 *
862 * package-protection for use by SharedSecrets.
863 */
864 native int getStackTraceDepth();
865
866 /**
867 * Returns the specified element of the stack trace.
868 *
869 * package-protection for use by SharedSecrets.
870 *
871 * @param index index of the element to return.
872 * @throws IndexOutOfBoundsException if {@code index < 0 ||
873 * index >= getStackTraceDepth() }
940 // older JDK releases; treat such exceptions as having
941 // empty stack traces.
942 stackTrace = UNASSIGNED_STACK.clone();
943 }
944 }
945
946 /**
947 * Write a {@code Throwable} object to a stream.
948 *
949 * A {@code null} stack trace field is represented in the serial
950 * form as a one-element array whose element is equal to {@code
951 * new StackTraceElement("", "", null, Integer.MIN_VALUE)}.
952 */
953 private synchronized void writeObject(ObjectOutputStream s)
954 throws IOException {
955 // Ensure that the stackTrace field is initialized to a
956 // non-null value, if appropriate. As of JDK 7, a null stack
957 // trace field is a valid value indicating the stack trace
958 // should not be set.
959 getOurStackTrace();
960
961 StackTraceElement[] oldStackTrace = stackTrace;
962 try {
963 if (stackTrace == null)
964 stackTrace = SentinelHolder.STACK_TRACE_SENTINEL;
965 s.defaultWriteObject();
966 } finally {
967 stackTrace = oldStackTrace;
968 }
969 }
970
971 /**
972 * Appends the specified exception to the exceptions that were
973 * suppressed in order to deliver this exception. This method is
974 * typically called (automatically and implicitly) by the {@code
975 * try}-with-resources statement.
976 *
977 * <p>The suppression behavior is enabled <em>unless</em> disabled
978 * {@linkplain #Throwable(String, Throwable, boolean, boolean) via
979 * a constructor}. When suppression is disabled, this method does
980 * nothing other than to validate its argument.
981 *
982 * <p>Note that when one exception {@linkplain
983 * #initCause(Throwable) causes} another exception, the first
984 * exception is usually caught and then the second exception is
985 * thrown in response. In other words, there is a causal
986 * connection between the two exceptions.
987 *
988 * In contrast, there are situations where two independent
|