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

Print this page

        

@@ -846,14 +846,13 @@
         for (int i = 0; i < defensiveCopy.length; i++) {
             if (defensiveCopy[i] == null)
                 throw new NullPointerException("stackTrace[" + i + "]");
         }
 
+        synchronized (this) {
         if (this.stackTrace == null) // Immutable stack
             return;
-
-        synchronized (this) {
             this.stackTrace = defensiveCopy;
         }
     }
 
     /**

@@ -956,22 +955,19 @@
         // Ensure that the stackTrace field is initialized to a
         // non-null value, if appropriate.  As of JDK 7, a null stack
         // trace field is a valid value indicating the stack trace
         // should not be set.
         getOurStackTrace();
-        ObjectOutputStream.PutField fields = s.putFields();
 
-        fields.put("detailMessage", detailMessage);
-        fields.put("cause", cause);
-        // Serialize a null stacktrace using the stack trace sentinel.
+        StackTraceElement[] oldStackTrace = stackTrace;
+        try {
         if (stackTrace == null)
-            fields.put("stackTrace", SentinelHolder.STACK_TRACE_SENTINEL);
-        else
-            fields.put("stackTrace", stackTrace);
-        fields.put("suppressedExceptions", suppressedExceptions);
-
-        s.writeFields();
+                stackTrace = SentinelHolder.STACK_TRACE_SENTINEL;
+            s.defaultWriteObject();
+        } finally {
+            stackTrace = oldStackTrace;
+        }
     }
 
     /**
      * Appends the specified exception to the exceptions that were
      * suppressed in order to deliver this exception. This method is