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

简介: public static String outputThrowable(Throwable t) throws FileNotFoundException{ for (StackTraceElement ste:t.

 

    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
0
0
0
95
分享
相关文章
FeignClient打印请求失败的日志,打印所有feignCliet接口请求失败的错误日志,方便排查原因
FeignClient打印请求失败的日志,打印所有feignCliet接口请求失败的错误日志,方便排查原因
292 0
日志框架log4j打印异常堆栈信息携带traceId,方便接口异常排查
日常项目运行日志,异常栈打印是不带traceId,导致排查问题查找异常栈很麻烦。
|
10月前
如何处理代理的404错误
如何处理代理的404错误
430 8
如何处理网站被植入恶意的一些代码导致的被机房拦截提示
如何处理网站被植入恶意的一些代码导致的被机房拦截提示
304 0
如何处理网站被植入恶意的一些代码导致的被机房拦截提示
301重定向是什么?301重定向怎么做?
页面永久性移走(301重定向)是一种非常重要的“自动转向”技术。网址重定向最为可行的一种办法。当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种,表示本网页永久性转移到另一个地址。 301永久重定向对SEO无任何不好的影响,而且网页A的的权重都会传达给网页B,对于搜索引擎优化、网站优化来说,给搜索引擎一个友好的信息,告诉它此页面已永久重定向,避免搜索引擎找不到页面。
331 0