【问题标题】:Check for Broken Pipe only when IOException is thrown仅在抛出 IOException 时检查 Broken Pipe
【发布时间】:2015-04-01 11:58:04
【问题描述】:

写入OutputStream 时,如果远程机器关闭或已断开连接,它将抛出IOException 异常。现在,我想在仅(且仅)管道损坏时调用特定方法。问题是IOException 没有codeerrno 属性来检查此错误。而我能够做到这一点的唯一方法是

        try {
            stream.write(message.getBytes("UTF8"));
            stream.flush();
        } catch (IOException e) {
            if (e.getMessage().equals("sendto failed: EPIPE (Broken pipe)")) {
                // perform a specific action
            } else {
                e.printStackTrace();
            }
        }

显然这不是实现这一目标的完美方式。有什么有效解决方案的建议吗?

【问题讨论】:

    标签: java android ioexception broken-pipe


    【解决方案1】:

    As mentioned here

    在 Java 中,没有专门的 BrokenPipeException。这类 将发现错误包含在不同的异常中,例如 SocketExceptionIOException

    所以你可以使用String#contains() 方法实现同样的效果

    if(e.getMessage().contains("Broken Pipe")){
    // Do Whatever you want to do 
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 2010-11-13
      • 2016-05-08
      • 2014-05-02
      • 2016-08-02
      相关资源
      最近更新 更多