使用此代码:
class SimpleException extends Exception {}
public class SimpleExceptionDemo {
public void f() throws SimpleException {
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
public static void main(String[] args) {
SimpleExceptionDemo sed = new SimpleExceptionDemo();
try {
sed.f();
} catch(SimpleException e) {
System.err.println("Caught it!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我有这个输出:
Caught it!
Throw SimpleException from f()
Run Code Online (Sandbox Code Playgroud)
你知道为什么"抓住它后"会打印出"从f()中抛出SimpleException"吗?
您正在两个不同的输出流上打印:
System.out.println("Throw SimpleException from f()");
Run Code Online (Sandbox Code Playgroud)
和
System.err.println("Caught it!");
Run Code Online (Sandbox Code Playgroud)
不保证从两个不同的流中出现的消息的顺序...使用相同的流,它就可以了.
如果您对这些问题感兴趣,可能会有兴趣阅读
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |