Reputation: 6290
I have written a Java client-server application using Sockets. I'm using the method writeUnshared()
ofObjectOutputStream
and readObject()
of ObjectInputStream
.
Now I would like to detect when the connection is broken to reconnnect (e.g. when Wifi signal was lost). Currently, for ObjectInputStream
I'm catching EOFException
and for ObjectOutputStream
I'm catching IOException
to detect if the pipe is borken.
Is this correct or should I catch IOException
for both streams?
Upvotes: 0
Views: 211
Reputation: 4605
You can go by the Javadoc, which recommends catching IOException for all of the usual I/O related exceptions.
Or you can go by your own testing.
Or you can go by the source code, which shows that when using "block data" mode EOFException is used by serialization whenever there is insufficient bytes to construct a full object. However, that would not catch all cases in which the connection is broken.
All things considered, I would recommend IOException.
Upvotes: 1