Difference between e.printStackTrace and System.out.println(e)

前端 未结 6 1589
清歌不尽
清歌不尽 2021-01-31 17:05

Probably a newbie question, but everyone seems to use e.printStackTrace(), but I have always used System.out.println(e) when exception handling. What i

6条回答
  •  你的背包
    2021-01-31 17:27

    Bellow Program show the difference details

    System.out.println(e); :- only showing the exception. e.g.java.lang.ArithmeticException: / by zero e.printStackTrace(); :- showing the details of exception and where to cause exception in program with line number e.g. java.lang.ArithmeticException: / by zero at test.Test.main(Test.java:7)

    package test;
    
    public class Test {
    
        public static void main(String args[]) {
            try {
                System.out.println(12 / 0);
    
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
    
        }
    }
    

提交回复
热议问题