jaxp/src/java.xml/share/classes/javax/xml/transform/TransformerException.java
changeset 31497 4a6b2e733c0d
parent 25868 686eef1e7a79
child 46005 49e2e73f90f6
--- a/jaxp/src/java.xml/share/classes/javax/xml/transform/TransformerException.java	Wed Jul 05 20:39:43 2017 +0200
+++ b/jaxp/src/java.xml/share/classes/javax/xml/transform/TransformerException.java	Tue Jun 30 12:04:27 2015 +0200
@@ -110,6 +110,12 @@
      */
     public synchronized Throwable initCause(Throwable cause) {
 
+        // TransformerException doesn't set its cause (probably
+        // because it predates initCause()) - and we may not want
+        // to change this since Exceptions are serializable...
+        // But this also leads to the broken code in printStackTrace
+        // below...
+
         if (this.containedException != null) {
             throw new IllegalStateException("Can't overwrite cause");
         }
@@ -312,61 +318,57 @@
         }
 
         try {
-            String locInfo = getLocationAsString();
+            try {
+                String locInfo = getLocationAsString();
 
-            if (null != locInfo) {
-                s.println(locInfo);
-            }
+                if (null != locInfo) {
+                    s.println(locInfo);
+                }
 
-            super.printStackTrace(s);
-        } catch (Throwable e) {}
+                super.printStackTrace(s);
+            } catch (Throwable e) {}
 
-        Throwable exception = getException();
+            Throwable exception = getException();
 
-        for (int i = 0; (i < 10) && (null != exception); i++) {
-            s.println("---------");
+            for (int i = 0; (i < 10) && (null != exception); i++) {
+                s.println("---------");
 
-            try {
-                if (exception instanceof TransformerException) {
-                    String locInfo =
-                        ((TransformerException) exception)
-                            .getLocationAsString();
-
-                    if (null != locInfo) {
-                        s.println(locInfo);
-                    }
+                try {
+                    exception.printStackTrace(s);
+                    // if exception is a TransformerException it will print
+                    // its contained exception, so we don't need to redo it here,
+                    // and we can exit the loop now.
+                    if (exception instanceof TransformerException) break;
+                } catch (Throwable e) {
+                    s.println("Could not print stack trace...");
                 }
 
-                exception.printStackTrace(s);
-            } catch (Throwable e) {
-                s.println("Could not print stack trace...");
-            }
+                try {
+                    // Is this still needed?
+                    Method meth = exception.getClass().getMethod("getException");
 
-            try {
-                Method meth =
-                    ((Object) exception).getClass().getMethod("getException",
-                        (Class[]) null);
+                    if (null != meth) {
+                        Throwable prev = exception;
+
+                        exception = (Throwable) meth.invoke(exception, (Object[]) null);
 
-                if (null != meth) {
-                    Throwable prev = exception;
-
-                    exception = (Throwable) meth.invoke(exception, (Object[]) null);
-
-                    if (prev == exception) {
-                        break;
+                        if (prev == exception) {
+                            break;
+                        }
+                    } else {
+                        exception = null;
                     }
-                } else {
+                } catch (InvocationTargetException ite) {
+                    exception = null;
+                } catch (IllegalAccessException iae) {
+                    exception = null;
+                } catch (NoSuchMethodException nsme) {
                     exception = null;
                 }
-            } catch (InvocationTargetException ite) {
-                exception = null;
-            } catch (IllegalAccessException iae) {
-                exception = null;
-            } catch (NoSuchMethodException nsme) {
-                exception = null;
             }
+        } finally {
+            // ensure output is written
+            s.flush();
         }
-        // insure output is written
-        s.flush();
     }
 }