Creating Java Exception getStackTrace() as a string
Dec 17, 2012 in Java, language
public class t1 { public static void main(String[] args) { final Throwable t = new IllegalArgumentException("hi!"); StringBuffer sb = new StringBuffer(); for (int tx = 0 ; tx < t.getStackTrace().length ; tx ++ ) { sb.append(t.getStackTrace()[tx] ); sb.append("\n"); } System.out.println(sb); } }
or, simpler:
System.out.println("STACK: [[" + java.util.Arrays.toString(Thread.currentThread().getStackTrace() ).replace(",", "\n") + "]]" );
. . .