且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

e.printStackTrace和System.out.println(e)之间的区别

更新时间:2022-03-19 04:35:26

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)
在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");
}