Package Torello.Java
Class EXCC
- java.lang.Object
-
- Torello.Java.EXCC
-
public class EXCC extends java.lang.Object
'Exception Cause Chain'helps convert exception messages whoseThrowable.cause()method returns a non-nullcause, thereby unrolling (and printing) this chain of exceptions into a readableString.
EXCC: Exception Cause Chain
In Java, there are exceptions that may have been thrown by a different thread. In Java,Exception'sthrown by a different thread will often be wrapped into anotherException, with the original'cause'Exception is kept by the Surrounding / 'Wrapping' Exception. It may be retrieved using theThrowableMethodgetCause().
Java does not expect every exception throw to provide a "cause" - quite the contrary, most do not. There are also a number of javaException'sthat 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 simpletry-catchblocks will catch an exceptions and wrap it (and throw the newExceptioninstance) in order to give theExceptiona 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:
ex.printStackTrace();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). TheString-Methods in this class use recursion to traverse through each "parent exception", adding indentations, until a null is returned when invokinggetCause().
Hi-Lited Source-Code:- View Here: Torello/Java/EXCC.java
- Open New Browser-Tab: Torello/Java/EXCC.java
File Size: 17,415 Bytes Line Count: 389 '\n' Characters Found
Stateless Class:This class neither contains any program-state, nor can it be instantiated. The@StaticFunctionalAnnotation may also be called 'The Spaghetti Report'.Static-Functionalclasses are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's@StatelessAnnotation.
- 1 Constructor(s), 1 declared private, zero-argument constructor
- 8 Method(s), 8 declared static
- 0 Field(s)
-
-
Method Summary
Print Exception and, recursively, all causes to a String Modifier and Type Method static StringtoString(Throwable t)static StringtoString(Throwable t, int indentation)Print Exceptions recursively, but do not print Stack-Traces Modifier and Type Method static StringtoStringNoST(Throwable t)static StringtoStringNoST(Throwable t, int indentation)Print Exceptions recursively, and limit number of Stack-Trace Entries to Print Modifier and Type Method static StringtoStringMaxTraces(Throwable t, int maxNumInvocations)static StringtoStringMaxTraces(Throwable t, int maxNumInvocations, int indentation)Protected Helpers: Handle Legacy Exception Cause Printing Modifier and Type Method protected static ThrowablelegacyCause(Throwable t)protected static StringlegacyMsg(Throwable t)
-
-
-
Method Detail
-
toString
public static java.lang.String toString(java.lang.Throwable t, int indentation)
- Code:
- Exact Method Body:
return StrIndent.indentTabs(toString(t), indentation);
-
toString
public static java.lang.String toString(java.lang.Throwable t)
Prints theException-message andException-stack trace to an outputString, and invokes, recursively, this method with any cause-Throwable'sthat are present in thisThrowable.- Parameters:
t- Any JavaThrowable. JavaThrowable'swith one or'cause'Throwable'swill utilize more fully the printing-features of this class,'EXCC'- though anyThrowablewill be properly printed.- Returns:
- This method doesn't actually print anything to the screen or terminal, it just
returns a
Stringthat you may print yourself - or write to aclass 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(); final Throwable legacyCause = legacyCause(t); final boolean hasCause = (cause != null); final boolean hasLegacyCause = (legacyCause != null) && (legacyCause != cause); String msg = t.getMessage(); String localMsg = t.getLocalizedMessage(); String legacyMsg = (hasLegacyCause) ? legacyMsg(t) : null; final boolean hasMessage = (msg != null) && (msg.length() > 0); final boolean hasLocalMsg = (localMsg != null) && (localMsg.length() > 0); if (hasMessage) msg = StrIndent.indent(msg, 1, false, true); if (hasLocalMsg) localMsg = StrIndent.indent(localMsg, 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 + msg + "\n"); if (hasLocalMsg && (! msg.equals(localMsg))) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + localMsg + "\n"); } else if (hasLocalMsg) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + localMsg + "\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 sb.toString() + (hasCause ? StrIndent.indentTabs(toString(cause), 1) : "") + (hasLegacyCause ? (legacyMsg + StrIndent.indentTabs(toString(legacyCause), 1)) : "");
-
toStringMaxTraces
public static java.lang.String toStringMaxTraces(java.lang.Throwable t, int maxNumInvocations, int indentation)
Convenience Method
Invokes:toStringMaxTraces(Throwable, int)
And-Then:StrIndent.indentTabs(String, int)- Code:
- Exact Method Body:
return StrIndent.indent(toStringMaxTraces(t, maxNumInvocations), indentation);
-
toStringMaxTraces
public static java.lang.String toStringMaxTraces(java.lang.Throwable t, int maxNumInvocations)
Prints theException-message andException-stack trace to an outputString, and invokes, recursively, this method with any cause-Throwable'sthat are present in thisThrowable.- Parameters:
t- Any JavaThrowable. JavaThrowable'swith one or'cause'Throwable'swill utilize more fully the printing-features of this class,'EXCC'- though anyThrowablewill be properly printed.maxNumInvocations- Each of theThrowable'sprinted shall have, at most,'maxNumInvocations'of their Stack-Traces printed into the outputString.- Returns:
- This method doesn't actually print anything to the screen or terminal, it just
returns a
Stringthat you may print yourself - or write to aclass 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(); final Throwable legacyCause = legacyCause(t); final boolean hasCause = (cause != null); final boolean hasLegacyCause = (legacyCause != null) && (legacyCause != cause); String msg = t.getMessage(); String localMsg = t.getLocalizedMessage(); String legacyMsg = (hasLegacyCause) ? legacyMsg(t) : null; final boolean hasMessage = (msg != null) && (msg.length() > 0); final boolean hasLocalMsg = (localMsg != null) && (localMsg.length() > 0); if (hasMessage) msg = StrIndent.indent(msg, 1, false, true); if (hasLocalMsg) localMsg = StrIndent.indent(localMsg, 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 + msg + "\n"); if (hasLocalMsg && (! msg.equals(localMsg))) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + localMsg + "\n"); } else if (hasLocalMsg) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + localMsg + "\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 sb.toString() + (hasCause ? StrIndent.indentTabs(toString(cause), 1) : "") + (hasLegacyCause ? (legacyMsg + StrIndent.indentTabs(toString(legacyCause), 1)) : "");
-
toStringNoST
public static java.lang.String toStringNoST(java.lang.Throwable t, int indentation)
- Code:
- Exact Method Body:
return StrIndent.indentTabs(toStringNoST(t), indentation);
-
toStringNoST
public static java.lang.String toStringNoST(java.lang.Throwable t)
Prints theException-message to an outputString. Invokes, recursively, this method with any cause-Throwable'sthat are present in thisThrowable.This method differs fromtoString(Throwable), in that it does not print theStackTrace'sto the output-returnString.- Parameters:
t- Any JavaThrowable. JavaThrowable'swith one or'cause'Throwable'swill utilize more fully the printing-features of this class,'EXCC'- though anyThrowablewill be properly printed.- Returns:
- This method doesn't actually print anything to the screen or terminal, it just
returns a
Stringthat you may print yourself - or write to aclass 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(); final Throwable legacyCause = legacyCause(t); final boolean hasCause = (cause != null); final boolean hasLegacyCause = (legacyCause != null) && (legacyCause != cause); String msg = t.getMessage(); String localMsg = t.getLocalizedMessage(); String legacyMsg = (hasLegacyCause) ? legacyMsg(t) : null; final boolean hasMessage = (msg != null) && (msg.length() > 0); final boolean hasLocalMsg = (localMsg != null) && (localMsg.length() > 0); if (hasMessage) msg = StrIndent.indent(msg, 1, false, true); if (hasLocalMsg) localMsg = StrIndent.indent(localMsg, 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 + msg + "\n"); if (hasLocalMsg && (! msg.equals(localMsg))) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + localMsg + "\n"); } else if (hasLocalMsg) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + localMsg + "\n"); else sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET); // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Build the StringBuilder & Return / Exit // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** return sb.toString() + (hasCause ? StrIndent.indentTabs(toStringNoST(cause), 1) : "") + (hasLegacyCause ? (legacyMsg + StrIndent.indentTabs(toStringNoST(legacyCause), 1)) : "");
-
legacyCause
protected static java.lang.Throwable legacyCause(java.lang.Throwable t)
Helper Method for retrieving exceptions that function in a very similar fashion to a'cause'exception.- Parameters:
t- Any Java Throwable- Returns:
- For legacy exception classes, this will return the
'cause', using the older method for extracting that cause. - Code:
- Exact Method Body:
if (t instanceof InvocationTargetException) return ((InvocationTargetException) t).getTargetException(); if (t instanceof ExceptionInInitializerError) return ((ExceptionInInitializerError) t).getException(); return null;
-
legacyMsg
protected static java.lang.String legacyMsg(java.lang.Throwable t)
Generates a simple descriptiveStringwhenever there is a cause exception present that has been extracted from one of the older exception classes that can produce such legacy causes.- Parameters:
t-- Returns:
- A descriptive
Stringexplaining the legacy cause feature - Code:
- Exact Method Body:
if (t instanceof InvocationTargetException) return "InvocationTargetException.getTargetException():\n"; if (t instanceof ExceptionInInitializerError) return "ExceptionInInitializerError.getException():\n"; return null;
-
-