chrisapotek
chrisapotek

Reputation: 6227

How to catch the IOException "Connection reset by peer"?

Parsing the string message seems bad. Or was this exception not meant to be caught?

java.io.IOException: Connection reset by peer
    at sun.nio.ch.FileDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:21)

Upvotes: 8

Views: 6309

Answers (2)

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

The JVM makes no difference about the real cause of the error, hence it will throw a generic IOException in any case (adapter disconnected, timeout, ICMP error...). Only the human readable string describes the error and there is nothing you can do about it.

Upvotes: 1

lucian.pantelimon
lucian.pantelimon

Reputation: 3669

You need to catch the IOException and, yes, parse the string.

There isn't any other exception more specific than that to be caught as far as I know.

Or catch SocketException and, again, parse the string.

Here's the class tree from javadoc (version 6). As you can see, SocketException is the most specialized exception to be thrown when the connection is reset.

Upvotes: 4

Related Questions