【问题标题】:IOException inside print or printlnprint 或 println 中的 IOException
【发布时间】:2011-04-20 07:11:41
【问题描述】:

大部分 IO 操作都需要 try catch 块或 throws 。为什么 System.out.print 或 println 不需要 try catch 或 throws。如果这些方法中有任何异常,我怎么知道异常是什么以及如何捕获它。

【问题讨论】:

    标签: java exception-handling io


    【解决方案1】:

    你可以通过调用来检查错误

    System.out.checkError();
    

    javadoc of PrintStream.checkError() 说:

    返回true 当且仅当此流遇到 IOException 以外 InterruptedIOException,或 setError 方法已被调用

    如果您真的想监控System.out 的异常,您可以将自己的PrintStream 设置为System.out 并覆盖您感兴趣的方法。

    【讨论】:

    • System.out.checkError();如果有异常则返回 true,否则返回 false,我怎么知道是哪个异常。
    • 你查不到。异常被静默捕获并设置故障标志:catch (IOException x) { trouble = true; }
    猜你喜欢
    • 2014-07-27
    • 2014-11-15
    • 2015-01-27
    • 1970-01-01
    • 2021-08-18
    • 2020-08-02
    • 2011-12-21
    • 2019-10-21
    相关资源
    最近更新 更多