0

I am attempting to catch a SocketTimeoutException so that my socket remains active and I can continue reading through my program. Unfortunately, no matter how I attempt to catch the read timeout I can't and therefore the application closes.

I have a socket and setsotimeout to 30000.

I call input.readLine() and want to catch the timeout and continue. Any help is much appreciated.

String response;
try {
        response = input.readLine();
        System.out.println("Server Response: " + response);
        return;
} catch(AssertionError e){
    //we timed out. print to user that timeout occured,
    //try command again
    System.out.println("TIMEOUT: Please try your command again.\n"
        + "If you created a game, join an existing or create again.");
} catch (SocketTimeoutException e) {
    //we timed out. print to user that timeout occured,
    //try command again
    System.out.println("TIMEOUT: Please try your command again.\n"
        + "If you created a game, join an existing or create again.");
} catch (IOException ex) {
//do nothing
}

the output is:

java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Client.main(Client.java:97)
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
Elliott
  • 147
  • 2
  • 11
  • what is in Client.java:97? – Rod_Algonquin Jun 01 '14 at 01:44
  • It is the input.readLine() call. I don't mind the timeout.... I am purposely wanting a timeout after 30 seconds. But I don't want my application to stop because of it. I want to catch that timeout and just continue on my way. – Elliott Jun 01 '14 at 05:38
  • Ah actually, its a different readLine(). Thanks for your help. I didn't have the socketException in the right catch block. That'll be the cause. – Elliott Jun 01 '14 at 05:41

1 Answers1

0

Either this isn't the real code, you have your own SocketTimeoutException class somewhere, or you've imported the wrong one. I suggest you put something useful into the catch IOException block. You might get a surprise.

Why you're catching AssertionError and treating it as a timeout is a mystery to me.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I originally was just catching SocketException, however I read online about some Assertionerror and threw it in as a what the heck. At this point, I'm completely baffled as to why i can't catch the timeout. – Elliott Jun 01 '14 at 05:07