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's
thrown 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 theThrowable
MethodgetCause()
.
Java does not expect every exception throw to provide a "cause" - quite the contrary, most do not. There are also a number of javaException'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 simpletry-catch
blocks will catch an exceptions and wrap it (and throw the newException
instance) in order to give theException
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:
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: 9,650 Bytes Line Count: 236 '\n' Characters Found
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)
-
-
-
Method Detail
-
toString
public static java.lang.String toString(java.lang.Throwable t, int 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's
that are present in thisThrowable
.- Parameters:
t
- Any JavaThrowable
. JavaThrowable's
with one or'cause'
Throwable's
will utilize more fully the printing-features of this class,'EXCC'
- though anyThrowable
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 aclass StringBuilder
, or any variation of text-output you wish. - Code:
- Exact Method Body:
StackTraceElement[] steArr = t.getStackTrace(); StringBuilder sb = new StringBuilder(); Throwable cause = t.getCause(); String m = t.getMessage(); String lm = t.getLocalizedMessage(); boolean hasMessage = (m != null) && (m.length() > 0); boolean hasLMess = (lm != null) && (lm.length() > 0); if (hasMessage) m = StrIndent.indent(m, 1, false, true); if (hasLMess) lm = StrIndent.indent(lm, 1, false, true); sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n"); if (hasMessage) { sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n"); if (hasLMess && (! m.equals(lm))) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n"); } else if (hasLMess) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n"); else sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET); sb.append(BCYAN + "StackTrace:\n" + RESET); int temp, maxLN = 0; for (StackTraceElement ste : steArr) if ((temp = ste.getLineNumber()) > maxLN) maxLN = temp; 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" ); 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, int indentation)
Convenience Method
Invokes:toStringMaxTraces(Throwable, int)
And-Then:StrIndent.indentTabs(String, int)
-
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's
that are present in thisThrowable
.- Parameters:
t
- Any JavaThrowable
. JavaThrowable's
with one or'cause'
Throwable's
will utilize more fully the printing-features of this class,'EXCC'
- though anyThrowable
will be properly printed.maxNumInvocations
- Each of theThrowable's
printed 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
String
that you may print yourself - or write to aclass StringBuilder
, or any variation of text-output you wish. - Code:
- Exact Method Body:
StackTraceElement[] steArr = t.getStackTrace(); StringBuilder sb = new StringBuilder(); Throwable cause = t.getCause(); String m = t.getMessage(); String lm = t.getLocalizedMessage(); boolean hasMessage = (m != null) && (m.length() > 0); boolean hasLMess = (lm != null) && (lm.length() > 0); if (hasMessage) m = StrIndent.indent(m, 1, false, true); if (hasLMess) lm = StrIndent.indent(lm, 1, false, true); sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n"); if (hasMessage) { sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n"); if (hasLMess && (! m.equals(lm))) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n"); } else if (hasLMess) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n"); else sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET); sb.append(BCYAN + "StackTrace:\n" + RESET); int temp, maxLN = 0; int 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; 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" ); if (stTruncated > 0) sb.append("\t... and " + stTruncated + " more invocations.\n"); return (cause == null) ? sb.toString() : sb.toString() + StrIndent.indentTabs(toString(cause), 1);
-
toStringNoST
public static java.lang.String toStringNoST(java.lang.Throwable t, int 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's
that are present in thisThrowable
.
This method differs fromtoString(Throwable)
, in that it does not print theStackTrace's
to the outputString
.- Parameters:
t
- Any JavaThrowable
. JavaThrowable's
with one or'cause'
Throwable's
will utilize more fully the printing-features of this class,'EXCC'
- though anyThrowable
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 aclass StorageBuffer,
or any variation of logging you wish. - Code:
- Exact Method Body:
StringBuilder sb = new StringBuilder(); Throwable cause = t.getCause(); String m = t.getMessage(); String lm = t.getLocalizedMessage(); boolean hasMessage = (m != null) && (m.length() > 0); boolean hasLMess = (lm != null) && (lm.length() > 0); if (hasMessage) m = StrIndent.indent(m, 1, false, true); if (hasLMess) lm = StrIndent.indent(lm, 1, false, true); sb.append(BRED + "THROWN: " + t.getClass().getCanonicalName() + RESET + "\n"); if (hasMessage) { sb.append(BCYAN + "Throwable.getMessage():\n" + RESET + m + "\n"); if (hasLMess && (! m.equals(lm))) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n"); } else if (hasLMess) sb.append(BCYAN + "Throwable.getLocalizedMessage():\n" + RESET + lm + "\n"); else sb.append(BYELLOW + "No Exception Messages Provided.\n" + RESET); return (cause == null) ? sb.toString() : sb.toString() + StrIndent.indentTabs(toStringNoST(cause), 1);
-
-