java中的多个try / catch执行之间是否有任何优先级?

时间:2012-05-03 18:31:58

标签: java

在下面的程序中,有时我得到以下输出:

Number Format Execption For input string: "abc"
123

有时候:

123
Number Format Execption For input string: "abc"

try / catch块或System.out和System.err之间的优先级是否有任何优先级?

随机输出的原因是什么?

代码:

String str1 = "abc";
String str2 = "123";

     try{
         int firstInteger = Integer.parseInt(str1);
         System.out.println(firstInteger);
     }
     catch(NumberFormatException e){
         System.err.println("Number Format Execption " + e.getMessage());
     }

       try{
         int SecondInteger = Integer.parseInt(str2);
         System.out.println(SecondInteger);
         }
     catch(NumberFormatException e){
         System.err.println("Number Format Execption " + e.getMessage());
     }

2 个答案:

答案 0 :(得分:12)

它与try / catch无关,与您编写System.out和System.err的内容有关;它们是两个不同的流,当它们写入控制台时,您无法控制它们的交错顺序。

答案 1 :(得分:0)

尝试明确刷新流,

System.out.flush();
System.err.flush();