0
点赞
收藏
分享

微信扫一扫

异常信息重定向及相关处理办法

代码小姐 2022-08-25 阅读 36

 

public static String outputThrowable(Throwable t) throws FileNotFoundException{
for (StackTraceElement ste:t.getStackTrace()) {
System.out.println(ste.getMethodName());
}
t.printStackTrace(System.err);
t.printStackTrace(System.out);
t.printStackTrace(new PrintStream("exception.txt"));//redirect to file

StringWriter sw=new StringWriter();
PrintWriter pw=new PrintWriter(sw);

try {
t.printStackTrace(pw);//redirect to file
} catch (Exception e) {
e.printStackTrace();
}finally{
pw.close();
}

return sw.toString();

}

Output:

main
java.lang.ArithmeticException: / by zero
at exception.TestException.main(TestException.java:11)
java.lang.ArithmeticException: / by zero
at exception.TestException.main(TestException.java:11)
java.lang.ArithmeticException: / by zero
at exception.TestException.main(TestException.java:11)
java.lang.ArithmeticException: / by zero
at exception.TestException.main(TestException.java:11)

 


 



举报

相关推荐

0 条评论