Java:了解输出,stderr输出

时间:2019-03-03 16:02:04

标签: java

  public class ExceptionDemo3 {
    public static void main(String[] args) {
        try {
            int a = 6, b = 0;
            int res = a / b;
            System.out.println("Integer division" + a + "/" + b + "=" + res);
        } catch (ArithmeticException e) {
             /*Displays the stack trace*/
            e.printStackTrace();
            System.out.println("Divide by zero error");
            /*Displays description of exception*/
            System.out.println(e);
        }
        System.out.println("After catch statement");
    }
}

每次执行该程序时,都会以不同的顺序输出输出。

运行1:

java.lang.ArithmeticException: / by zero           //displayed in red color
at demo.ExceptionDemo3.main(ExceptionDemo3.java:8) //displayed in red color
Divide by zero error
java.lang.ArithmeticException: / by zero
After catch statement

Run2:

Divide by zero error
java.lang.ArithmeticException: / by zero    //displayed in red color
java.lang.ArithmeticException: / by zero
After catch statement
at demo.ExceptionDemo3.main(ExceptionDemo3.java:8)//displayed in red color

我读到e.printStackTrace()将输出写入stderr。所以我尝试了e.printStackTrace(System.out),这每次给我结果都以相同的顺序(黑色)。

1)但是为什么这里给出的我的实际程序不一样?为什么顺序不同? 2)红色显示是否有错误?

谢谢。

0 个答案:

没有答案