1

I have a program which sometimes encounters the errorjava.net.SocketException. Is there a way I can have the client program execute some code if (and only if) it encounters this error in order to "deal with" the error? The full error is

java.net.SocketException: Software caused connection abort: socket write error

Here's a summary of what I have now.

public void run()  {
    try {
        //some code that causes the SocketException 
    }

    catch (SocketException e) {
    System.out.println("I recognize the SocketException");
    }
}

However despite the error it still won't print the line.

Here is the full error

java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)  
    at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at (...).java:61)
    at (...).java:164)
2
  • This question appears to be off-topic because it is too localized Commented Jul 31, 2013 at 23:21
  • Note that in this case the connection was dropped by your side, not by the peer, for network reliability reasons. See Official Reasons for 'software caused connection abort'. Commented Jul 31, 2013 at 23:51

1 Answer 1

1

It depends on whether the client or the server is the one throwing the exception.

Either way, you should just be able to add a try-catch block around the code.

try {
   //code
} catch (SocketException e) {
   // Handle the error
}

If the server is throwing the error and you want the client to handle the situation, the client should be throwing an exception due to the loss of connection (this is what this exception looks like). Alternatively, if the server throws an error but doesn't crash, just have the server send some signal to the client that an error occurred.

Either way, with a little more information, I could give you a better resposne.

Sign up to request clarification or add additional context in comments.

5 Comments

Well the client encounters the error (it appears in the client software's console) because it's lost the connection.
If that is the case then the try-catch block will work just fine.
I tried this it did not work. Please see my updated question.
I tried it just now with my own test code and it catches the exception just fine when the server resets. Are you sure you have the try block around the right code?
Yes you're correct I had it around the wrong code... thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.