e.printStackTrace和System.out.println(e)之间的区别 [英] Difference between e.printStackTrace and System.out.println(e)

查看:165
本文介绍了e.printStackTrace和System.out.println(e)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个新手问题,但是每个人似乎都使用 e.printStackTrace(),但是我一直使用 System.out.println(e )当异常处理。有什么区别,为什么 e.printStackTrace()优先?

Probably a newbie question, but everyone seems to use e.printStackTrace(), but I have always used System.out.println(e) when exception handling. What is the difference and why is e.printStackTrace() preferable?

推荐答案

p>所使用的输出流与@Brian指出的不一样,但细节级别也不尽相同,您可以尝试下面的简单测试。输出:

The output stream used is not the same as pointed out by @Brian, but the level of detail is not the same either - you can try with the simple test below. Output:

使用println:您只知道抛出什么异常

With println: you only know what exception has been thrown


java.lang.UnsupportedOperationException:尚未实现

java.lang.UnsupportedOperationException: Not yet implemented

使用printStackTrace:您也知道导致了什么(行号+调用堆栈) p>

With printStackTrace: you also know what caused it (line numbers + call stack)


java.lang.UnsupportedOperationException:尚未实现

在javaapplication27.Test1.test(Test1.java:27)< br>
在javaapplication27.Test1.main(Test1.java:19)

java.lang.UnsupportedOperationException: Not yet implemented
at javaapplication27.Test1.test(Test1.java:27)
at javaapplication27.Test1.main(Test1.java:19)



public static void main(String[] args){
    try {
        test();
    } catch (UnsupportedOperationException e) {
        System.out.println(e);
        e.printStackTrace();
    }
}

private static void test() {
    throw new UnsupportedOperationException("Not yet implemented");
}

这篇关于e.printStackTrace和System.out.println(e)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆