Mystacktrace:
/** * * @author wumingkun * @version 1.0.0 * @Description */package com.demo.stacktrace;import java.io.PrintStream;/** * @author wumingkun * */public class MyStackTrace {public static String getStackTrace(Throwable errors) {StringBuffer result = new StringBuffer();result.append(errors);StackTraceElement[] trace = errors.getStackTrace();for (int i = 0; i < trace.length; i++)result.append("\n\tat " + trace[i]); Throwable ourCause = errors.getCause(); if (ourCause != null) result.append(getStackTraceAsCause(ourCause, trace));return result.toString();}private static String getStackTraceAsCause(Throwable ourCause,StackTraceElement[] causedTrace) {StringBuffer result=new StringBuffer();StackTraceElement[] trace = ourCause.getStackTrace();int m = trace.length - 1, n = causedTrace.length - 1;while (m >= 0 && n >= 0 && trace[m].equals(causedTrace[n])) {m--;n--;}int framesInCommon = trace.length - 1 - m;result.append("\nCaused by: " + ourCause);for (int i = 0; i <= m; i++)result.append("\n\tat " + trace[i]);if (framesInCommon != 0)result.append("\n\t... " + framesInCommon + " more");Throwable tempCause = ourCause.getCause();if (tempCause != null)result.append(getStackTraceAsCause(tempCause, trace));return result.toString();}}
Stacktracetest:
/***** @ Author wumingkun * @ version 1.0.0 * @ Description */package COM. demo. stacktrace;/*** @ author wumingkun ***/public class stacktracetest {/*** @ Param ARGs */public static void main (string [] ARGs) {try {M1 ();} catch (exception e) {system. out. println (mystacktrace. getstacktrace (e); // returns the exception stack information string}/*****/Private Static void M1 () {stringbuffer sb = NULL; try {sb. append ("AA");} catch (exception e) {Throw new runtimeexception (e );}}}