Java 中的 try-catch 块 - catch 代码中的执行语句
我对 Java 中 catch 块中语句的执行顺序有疑问。 当我运行以下类 Test1 (见下文)时,我希望首先输出 Hi!,然后是 e.printStackTrace(); 的结果; 声明,然后再见! 然而,我从来没有收到过这个订单。 请查看我粘贴在下面的输出。
public class Test1 {
public static void calculate() {
try {
int h = 5/0;
} catch (ArithmeticException e) {
System.out.println("Hi!");
e.printStackTrace();
}
System.out.println("Bye!");
}
public static void main(String[] args) {
calculate();
}
}
输出1:
Hi! Bye! java.lang.ArithmeticException: / by zero at Test1.calculate(Test1.java:6) at Test1.main(Test1.java:15)
输出2:
java.lang.ArithmeticException: / by zero at Test1.calculate(Test1.java:6) at Test1.main(Test1.java:15) Hi! Bye!
我有两个问题:
1.)更重要的问题:为什么我总是有Hi! 再见! 总是一个接一个地打印,即使代码中的 mye.printStackTrace() 位于它们之间?
2.) 为什么有时我在 Hi! 之前有 e.printStackTrace() 语句的输出,有时在 Bye! 之后有输出 ? 我已经运行该程序多次,但我无法理解在什么情况下我会得到其中一张或另一张打印结果。
谢谢。
我正在使用 Java 6 和 Eclipse (Ganymed)。
I have a question about the order of the execution of statements in a catch block in Java.
when I run the following class Test1 (see below), I expect to have as output first Hi!, then the result of the e.printStackTrace(); statement, and then Bye!. However, I never get this order. Please, look at the outputs, which I have pasted below.
public class Test1 {
public static void calculate() {
try {
int h = 5/0;
} catch (ArithmeticException e) {
System.out.println("Hi!");
e.printStackTrace();
}
System.out.println("Bye!");
}
public static void main(String[] args) {
calculate();
}
}
Output1:
Hi! Bye! java.lang.ArithmeticException: / by zero at Test1.calculate(Test1.java:6) at Test1.main(Test1.java:15)
Output2:
java.lang.ArithmeticException: / by zero at Test1.calculate(Test1.java:6) at Test1.main(Test1.java:15) Hi! Bye!
I have two questions:
1.) The more important question: Why I always have Hi! and Bye! printed always one after the other, even though mye.printStackTrace() in the code is between them?
2.) Why sometimes I have the output of the statement e.printStackTrace() before Hi!, and sometimes after Bye! ? I have run the program many times and I cannot understand under what circumstances I get one or the other print.
Thank you.
I am using Java 6, and Eclipse (Ganymed).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你打印“嗨!” 和“再见!” 到
System.out
(即stdout),而堆栈跟踪被打印到System.err
(即stderr)。 它们的打印顺序由刷新两个缓冲区的时间决定。You print "Hi!" and "Bye!" to
System.out
(i.e. stdout), while the stack trace is printed toSystem.err
(i.e. stderr). The order in which they are printed is determined by when the two buffers are flushed.这可能是一个时间问题。 Println 写入标准输出,而 printStackTrace 可能连接到标准错误。 那么问题就在于先刷新哪个缓冲区了。
It could be a timing issue. Println writes to standard out, while printStackTrace might be hooked up to standard error. Then it's just a matter of which buffer gets flushed first.
A1 - e.printStackTrace() 打印到 System.err 而不是 System.out,因此不同的流,不同的打印顺序。
A1 - e.printStackTrace() prints to System.err and not System.out, so different streams, different print order.
尝试在每次打印后添加 System.out.flush() (包括 printStackTrace)。
try adding System.out.flush() after every print (printStackTrace included).
异常.printStackTrace()
打印到System.err
而“Hi!
”和“Bye!
”位于System.out
上。 如果您在常规控制台上运行程序,它们最终会出现在屏幕上,但顺序可能会改变。 如果您通过 IDE 运行程序(例如 NetBeans),则流可能会进行颜色编码,以便您可以很容易地区分它们。Exception.printStackTrace()
prints toSystem.err
whereas "Hi!
" and "Bye!
" are onSystem.out
. If you run your program on a regular console, they eventually end up on the screen, but the order may be out. If you are running the program through an IDE (e.g. NetBeans), the streams will probably be color-coded so you can easily distinguish them.