Naming conventions
Stack trace as String
Multiple return statements
result
'
for the return value of a method can increase legibility.
Example
import java.io.*; /** Simple utilities to return the stack trace of an exception as a String. */ public final class StackTraceUtil { public static String getStackTrace(Throwable throwable) { Writer result = new StringWriter(); PrintWriter printWriter = new PrintWriter(result); throwable.printStackTrace(printWriter); return result.toString(); } /** Defines a custom format for the stack trace as String. */ public static String getCustomStackTrace(Throwable throwable) { //add the class name and any message passed to constructor StringBuilder result = new StringBuilder( "BOO-BOO: " ); result.append(throwable.toString()); String NL = System.getProperty("line.separator"); result.append(NL); //add each element of the stack trace for (StackTraceElement element : throwable.getStackTrace()){ result.append(element); result.append(NL); } return result.toString(); } /** Demonstrate output. */ public static void main (String... args){ Throwable throwable = new IllegalArgumentException("Blah"); System.out.println(getStackTrace(throwable)); System.out.println(getCustomStackTrace(throwable)); } }