Package Torello.Java

Class EXCC


  • public class EXCC
    extends java.lang.Object
    'Exception Cause Chain' helps convert exception messages whose Throwable.cause() method returns a non-null cause, thereby unrolling (and printing) this chain of exceptions into a readable String.

    EXCC: Exception Cause Chain

    In Java, there are exceptions that may have been thrown by a different thread. In Java, Exception's thrown by a different thread will often be wrapped into another Exception, with the original 'cause' Exception is kept by the Surrounding / 'Wrapping' Exception. It may be retrieved using the Throwable Method getCause().

    Java does not expect every exception throw to provide a "cause" - quite the contrary, most do not. There are also a number of java Exception's that do not even allow for a "cause throwable" in any of the provided constructors. Multi-threaded code is on source of "exception chaining" - but code there are also instances where simple try-catchblocks will catch an exceptions and wrap it (and throw the new Exception instance) in order to give the Exception a different name, and also to provide more detailed information about the error that has occurred.

    Either way, printing such information is easier to read if there is a standardized formatting for such information, which is the purpose behind class 'EXCC'

    To view both stack-traces, invoke the following:

    1. ex.printStackTrace();
    2. ex.getCause().printStackTrace();

    If viewing the "getCause()" of an exception is important, this class will help print the cause-exception (and even the causes' cause - recursively). The String-Methods in this class use recursion to traverse through each "parent exception", adding indentations, until a null is returned when invoking getCause().



    Stateless Class:
    This class neither contains any program-state, nor can it be instantiated. The @StaticFunctional Annotation may also be called 'The Spaghetti Report'. Static-Functional classes are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's @Stateless Annotation.

    • 1 Constructor(s), 1 declared private, zero-argument constructor
    • 6 Method(s), 6 declared static
    • 0 Field(s)


    • Method Summary

       
      Print Exception and, recursively, all causes to a String
      Modifier and Type Method
      static String toString​(Throwable t)
      static String toString​(Throwable t, int indentation)
       
      Print Exceptions recursively, but do not print Stack-Traces
      Modifier and Type Method
      static String toStringNoST​(Throwable t)
      static String toStringNoST​(Throwable t, int indentation)
       
      Print Exceptions recursively, and limit number of Stack-Trace Entries to Print
      Modifier and Type Method
      static String toStringMaxTraces​(Throwable t, int maxNumInvocations)
      static String toStringMaxTraces​(Throwable t, int maxNumInvocations, int indentation)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toString

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.String toString​(java.lang.Throwable t)
        Prints the Exception-message and Exception-stack trace to an output String, and invokes, recursively, this method with any cause-Throwable's that are present in this Throwable.
        Parameters:
        t - Any Java Throwable. Java Throwable's with one or 'cause' Throwable's will utilize more fully the printing-features of this class, 'EXCC' - though any Throwable will be properly printed.
        Returns:
        This method doesn't actually print anything to the screen or terminal, it just returns a String that you may print yourself - or write to a class StringBuilder, or any variation of text-output you wish.
        Code:
        Exact Method Body:
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Initialize the Variables
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         final StackTraceElement[]   steArr  = t.getStackTrace();
         final StringBuilder         sb      = new StringBuilder();
         final Throwable             cause   = t.getCause();
        
         String m    = t.getMessage();
         String lm   = t.getLocalizedMessage();
        
         final boolean hasMessage    = (m != null) && (m.length() > 0);
         final boolean hasLMsg       = (lm != null) && (lm.length() > 0);
        
         if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
         if (hasLMsg)    lm  = StrIndent.indent(lm, 1, false, true);
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Print the "Top Part" / "Header Part" / Error-Message to the Return-Output StringBuilder
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n");
        
         if (hasMessage)
         {
             sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n");
        
             if (hasLMsg && (! m.equals(lm)))
                 sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
         }
        
         else if (hasLMsg)
             sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
        
         else
             sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET);
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Print the Stack-Trace
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         sb.append(BCYAN + "StackTrace:\n" + RESET);
        
         int temp, maxLN = 0;
        
         for (StackTraceElement ste : steArr)
             if ((temp = ste.getLineNumber()) > maxLN)
                 maxLN = temp;
        
         final int base10 =
             2 + // 2: colon + space
             ((int) Math.ceil(Math.log10(maxLN)));
        
         for (int k=0; k < steArr.length; k++) sb.append(
             '\t' + StringParse.rightSpacePad(steArr[k].getLineNumber() + ":", base10) +
             steArr[k].getClassName() + "." +
             steArr[k].getMethodName() + "()\n"
         );
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Build the StringBuilder & Return / Exit
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         return (cause == null)
             ? sb.toString()
             : sb.toString() + StrIndent.indentTabs(toString(cause), 1);
        
      • toStringMaxTraces

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.String toStringMaxTraces​(java.lang.Throwable t,
                                                         int maxNumInvocations)
        Prints the Exception-message and Exception-stack trace to an output String, and invokes, recursively, this method with any cause-Throwable's that are present in this Throwable.
        Parameters:
        t - Any Java Throwable. Java Throwable's with one or 'cause' Throwable's will utilize more fully the printing-features of this class, 'EXCC' - though any Throwable will be properly printed.
        maxNumInvocations - Each of the Throwable's printed shall have, at most, 'maxNumInvocations' of their Stack-Traces printed into the output String.
        Returns:
        This method doesn't actually print anything to the screen or terminal, it just returns a String that you may print yourself - or write to a class StringBuilder, or any variation of text-output you wish.
        Code:
        Exact Method Body:
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Initialize the Variables
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         StackTraceElement[] steArr = t.getStackTrace();
        
         final StringBuilder sb      = new StringBuilder();
         final Throwable     cause   = t.getCause();
        
         String m    = t.getMessage();
         String lm   = t.getLocalizedMessage();
        
         final boolean hasMessage    = (m != null) && (m.length() > 0);
         final boolean hasLMsg       = (lm != null) && (lm.length() > 0);
        
         if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
         if (hasLMsg)    lm  = StrIndent.indent(lm, 1, false, true);
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Print the "Top Part" / "Header Part" / Error-Message to the Return-Output StringBuilder
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n");
        
         if (hasMessage)
         {
             sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n");
        
             if (hasLMsg && (! m.equals(lm)))
                 sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
         }
        
         else if (hasLMsg)
             sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
        
         else
             sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET);
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Print the Stack-Trace
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         sb.append(BCYAN + "StackTrace:\n" + RESET);
        
         int temp, maxLN = 0, stTruncated = 0;
        
         if (steArr.length > maxNumInvocations)
         {
             stTruncated = steArr.length - maxNumInvocations;
             steArr      = Arrays.copyOf(steArr, maxNumInvocations);
         }
        
         for (StackTraceElement ste : steArr)
             if ((temp = ste.getLineNumber()) > maxLN)
                 maxLN = temp;
        
         final int base10 =
             2 + // 2: colon + space
             ((int) Math.ceil(Math.log10(maxLN)));
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Build the StringBuilder & Return / Exit
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         for (int k=0; k < steArr.length; k++) sb.append(
             '\t' + StringParse.rightSpacePad(steArr[k].getLineNumber() + ":", base10) +
             steArr[k].getClassName() + "." +
             steArr[k].getMethodName() + "()\n"
         );
        
         if (stTruncated > 0)
             sb.append("\t... and " + stTruncated + " more invocations.\n");
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Build the StringBuilder & Return / Exit
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         return (cause == null)
             ? sb.toString()
             : sb.toString() + StrIndent.indentTabs(toString(cause), 1);
        
      • toStringNoST

        🡅     🗕  🗗  🗖
        public static java.lang.String toStringNoST​(java.lang.Throwable t)
        Prints the Exception-message to an output String. Invokes, recursively, this method with any cause-Throwable's that are present in this Throwable.

        This method differs from toString(Throwable), in that it does not print the StackTrace's to the output-return String.
        Parameters:
        t - Any Java Throwable. Java Throwable's with one or 'cause' Throwable's will utilize more fully the printing-features of this class, 'EXCC' - though any Throwable will be properly printed.
        Returns:
        This method doesn't actually print anything to the screen or terminal, it just returns a String that you may print yourself - or write to a class StorageBuffer, or any variation of logging you wish.
        Code:
        Exact Method Body:
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Initialize the Variables
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         final StringBuilder sb      = new StringBuilder();
         final Throwable     cause   = t.getCause();
        
         String m    = t.getMessage();
         String lm   = t.getLocalizedMessage();
        
         final boolean hasMessage    = (m != null) && (m.length() > 0);
         final boolean hasLMsg       = (lm != null) && (lm.length() > 0);
        
         if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
         if (hasLMsg)    lm  = StrIndent.indent(lm, 1, false, true);
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Print the "Top Part" / "Header Part" / Error-Message to the Return-Output StringBuilder
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n");
        
         if (hasMessage)
         {
             sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n");
        
             if (hasLMsg && (! m.equals(lm)))
                 sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
         }
        
         else if (hasLMsg)
             sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n");
        
         else
             sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET);
        
        
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
         // Build the StringBuilder & Return / Exit
         // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
         return (cause == null)
             ? sb.toString()
             : sb.toString() + StrIndent.indentTabs(toStringNoST(cause), 1);