1. 程式人生 > 其它 >java獲取Exception報錯堆疊資訊,轉換成字串

java獲取Exception報錯堆疊資訊,轉換成字串

技術標籤:exception報錯資訊獲取報錯堆疊藍何忠

方法:e.printStackTrace傳入輸出目標引數即可。

package com.lan.test;

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

public class ExceptionStack {
	public static void main(String[] args) {
		try {
			System.out.println(Integer.valueOf("17.o"));
		} catch (Exception e) {
			final Writer result = new StringWriter();
			final PrintWriter printWriter = new PrintWriter(result);
			e.printStackTrace(printWriter);
			String stackTraceStr = result.toString();
			System.out.println(stackTraceStr);
		}
	}
}

結果: