Exception.printStackTrace()转换为String输出
2016-08-16 11:59
477 查看
package com.test1;
import java.io.PrintWriter;
import java.io.StringWriter;
public class T010 {
/**
* @param args
*/
public static void main(String[] args) {
try {
String[] arr = {"111", "222"};
arr[2] = "fff";
} catch (Exception e) {
String info = getErrorInfoFromException(e);
System.out.println(info);
}
}
public static String getErrorInfoFromException(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return "\r\n" + sw.toString() + "\r\n";
} catch (Exception e2) {
return "bad getErrorInfoFromException";
}
}
}
控制台上输出的消息如下所示:
java.lang.ArrayIndexOutOfBoundsException: 2
at com.test1.T010.main(T010.java:14)
import java.io.PrintWriter;
import java.io.StringWriter;
public class T010 {
/**
* @param args
*/
public static void main(String[] args) {
try {
String[] arr = {"111", "222"};
arr[2] = "fff";
} catch (Exception e) {
String info = getErrorInfoFromException(e);
System.out.println(info);
}
}
public static String getErrorInfoFromException(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return "\r\n" + sw.toString() + "\r\n";
} catch (Exception e2) {
return "bad getErrorInfoFromException";
}
}
}
控制台上输出的消息如下所示:
java.lang.ArrayIndexOutOfBoundsException: 2
at com.test1.T010.main(T010.java:14)
相关文章推荐
- Java如何将Exception.printStackTrace()转换为String输出
- Java如何将Exception.printStackTrace()转换为String输出
- Java如何将Exception.printStackTrace()转换为String输出
- java中exception的printStackTrace()输出内容转成字符串
- java中exception的printStackTrace()输出内容转成字符串
- java中exception的printStackTrace()输出内容转成字符串
- 使用日志框架如何输出printStackTrace()的堆栈信息
- Android中用LogCat输出e.printStackTrace
- printStackTrace()输出和System.out输出非调用顺序输出
- e.printStackTrace()与System.out输出顺序混乱
- 将printStackTrace信息转换为字符串。
- java 获取e.printStackTrace() 的具体信息,赋值给String 变量 并返回
- printStackTrace输出和System.out输出非调用顺序输出
- ART如何实现Exception.printStackTrace
- spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace
- 怎样把e.printStackTrace()这个异常保存到一个字符串变量中如:String str;
- 怎样把Exception的printStackTrace()打印到文件里
- JAVA Catch中异常信息的输出 System.out.println(e) e.printStackTrace()
- 使用log4j的时候如何输出printStackTrace()的堆栈信息
- - Redirect printStackTrace() to a String