Is there any priority between multiple try / catch executions in Java?
•
Java
In the following program, sometimes I get the following output:
Number Format Execption For input string: "abc" 123
occasionally:
123 Number Format Execption For input string: "abc"
Try / catch block or system Out and system Are there any priorities between errs?
What is the reason for random output?
Code:
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()); }
Solution
It is associated with try / catch and with writing to system Out and system All contents of err are irrelevant; They are two different streams, and you cannot control their interleaving order when they are written to the console
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码