写入时,如果远程机器关闭或已断开连接,OutputStream
它将引发异常。IOException
现在,我想在仅(且仅)管道损坏时调用特定方法。问题是IOException
没有code
orerrno
属性来检查这个错误。我能够做到这一点的唯一方法是
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();
}
}
显然,这不是实现这一目标的完美方式。对有效解决方案有什么建议吗?