Started learning java a week ago and also decided learning the right way to work with exceptions. Java really drives me mad with the idea of specifying exceptions the method can throw as part of its signature.
I'm currently trying to implement multi-threaded server for client-server application. I was very surprised with the fact socket.close()
can throw IOException. The question is, what do I do if it happens?
...
final Socket socket = .... // at this point I know that I have a good socket
try {
..... // communicating with someone on that side....
} catch(IOException e) {
// communication failed
// this fact is useful, I can log it
// and will just hang up
} finally {
try {
socket.close(); // bye-bye
} catch(IOException e) {
// anything but logging I can do here?
}
}
...
This piece of code is executed within a separate thread, so I just have to catch all the exceptions. The problem is mostly psychological - I know, I can just leave catch
block empty, but I'm trying to avoid this trick.
Any thoughts?