1. 程式人生 > >獲取異常的堆疊資訊

獲取異常的堆疊資訊

import java.io.PrintWriter;
import java.io.StringWriter;

public class ExceptionUtil {

/**
* 獲取異常的堆疊資訊
*
* @param t
* @return
*/
public static String getStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

try {
t.printStackTrace(pw);
return sw.toString();
} finally {
pw.close();
}
}
}