Author: techfox9

Creating Java Exception getStackTrace() as a string

Monday, December 17th, 2012 @ 3:52 pm


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") + "]]" );

. . .

Java, language


 


Comments are closed.