When writing to an OutputStream
it will throw a IOException
exception if the remote machine down, or has disconnected. Now, I want to call a specific method when only (and only) the pipe is broken. The problem is that IOException
does not have a code
or errno
property to check for this error. And the only way I was able to achieve this is by
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();
}
}
Obviously this is not the perfect way to achieve such a thing. Any suggestion for an efficient solution?