【发布时间】:2011-04-20 07:11:41
【问题描述】:
大部分 IO 操作都需要 try catch 块或 throws 。为什么 System.out.print 或 println 不需要 try catch 或 throws。如果这些方法中有任何异常,我怎么知道异常是什么以及如何捕获它。
【问题讨论】:
标签: java exception-handling io
大部分 IO 操作都需要 try catch 块或 throws 。为什么 System.out.print 或 println 不需要 try catch 或 throws。如果这些方法中有任何异常,我怎么知道异常是什么以及如何捕获它。
【问题讨论】:
标签: java exception-handling io
你可以通过调用来检查错误
System.out.checkError();
javadoc of PrintStream.checkError() 说:
返回
true当且仅当此流遇到IOException以外InterruptedIOException,或setError方法已被调用
如果您真的想监控System.out 的异常,您可以将自己的PrintStream 设置为System.out 并覆盖您感兴趣的方法。
【讨论】:
catch (IOException x) { trouble = true; }