Java:使用try-catch以普通和调试模式中的不同控制台输出
我是Java的新手,试图了解JVM的行为。好吧,我有一个简单的代码:
public class MyClass {
public static void main(String[] args) {
System.out.println(anotherMethod(5));
System.out.println(anotherMethod(0));
}
private static Integer anotherMethod(int x) {
try {
return 10 / x;
}
catch (ArithmeticException e) {
e.printStackTrace();
return 0;
}
}
}
在正常模式下,它将在控制台中返回:
2
0
java.lang.ArithmeticException: / by zero
at MyClass.anotherMethod(MyClass.java:9)
at MyClass.main(MyClass.java:4)
但是当我将断点放在行中时“ system.out.println(另一个method(5));”然后通过逐步执行(In Inde In In In In In In In In In In In In In In In In In In In In In In In In In In In In It)返回(我认为这是更正确的):
2
java.lang.ArithmeticException: / by zero
at MyClass.anotherMethod(MyClass.java:9)
at MyClass.main(MyClass.java:4)
0
请您可以解释为什么输出之间的输出彼此之间有所不同吗?从我的角度来看,它应该是相同的,并且对应于我的问题中提到的第二个输出。
非常感谢您!
I'm a novice in Java, trying to understand behaviour of JVM. Well, I have a simple code:
public class MyClass {
public static void main(String[] args) {
System.out.println(anotherMethod(5));
System.out.println(anotherMethod(0));
}
private static Integer anotherMethod(int x) {
try {
return 10 / x;
}
catch (ArithmeticException e) {
e.printStackTrace();
return 0;
}
}
}
In normal mode it returns in console:
2
0
java.lang.ArithmeticException: / by zero
at MyClass.anotherMethod(MyClass.java:9)
at MyClass.main(MyClass.java:4)
But when I place breakpoint in line "System.out.println(anotherMethod(5));" and then complete the program via step-by-step execution (F8 in Idea) in debugging mode it returns (and I think this is more correctly):
2
java.lang.ArithmeticException: / by zero
at MyClass.anotherMethod(MyClass.java:9)
at MyClass.main(MyClass.java:4)
0
Please, would you be able to explain why the output differs between each other? From my perspective it should be the same and correspond to the second output mentioned in my question.
Thank you so much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到的是由于您的代码输出到两个不同的输出流,
stdout
和stderr
。由于缓冲,很难知道系统何时决定根据输出的顺序实际输出这些流写的内容。解决此问题的最简单方法是将所有输出转到一个流。
printstacktrace()
方法采用一个可选的输出流参数,该参数使您可以指定堆栈跟踪输出进入stdout
而不是stderr
>。如果这样做,您将始终按期望的顺序获得输出:结果:
What you are seeing is due to the fact that your code is outputting to two different output streams,
stdout
andstderr
. Due to buffering, it is hard to know just when the system will decide to actually output what is written to these streams in terms of the order of what is output.The easiest way to solve this problem is to have all of your output go to a single stream. The
printStackTrace()
method takes an optional output stream argument, which lets you specify that you want the stack trace output to go tostdout
instead ofstderr
. If you do this, you will always get the output in the order you expect:Result:
好吧,如果您选择“运行”,则实际上是正确的名称是:“无需调试”。
因此,您正在跑步而没有调试。如果您想检查代码的每个细节,请使用调试。如果您只想看到结果而没有太多细节,请点击“无需调试”。
Well, if you choose "Run", actually the correct name is: "Run without Debugging".
So, you are running WITHOUT debugging. If you want to check every detail of your code, use Debugging. if you just want to see the result without much detail, hit "Run without Debugging".