Reputation: 9548
I want to disable one error message produces by a socket timeout, because it's filling my console (Linux, terminal).
The code:
public ThreadListenResponseQuery(DatagramSocket socket){
this.socket = socket;
try {
socket.setSoTimeout(1500);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run(){
System.out.println("Waiting for response...");
// Waiting for 60 seconds...
while(System.currentTimeMillis() < startTime + 60000){
socket.receive(receive_packet);
// .. Additional work
}
}
The program is waiting for 60 seconds to get any response. I am setting a timeout on the socket because the while
cycle is freezing out if no response message is coming.
Error:
java.net.SocketTimeoutException: Receive timed out at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method) at java.net.DualStackPlainDatagramSocketImpl.receive0(Unknown Source) at java.net.AbstractPlainDatagramSocketImpl.receive(Unknown Source) at java.net.DatagramSocket.receive(Unknown Source) at thread.ThreadListenResponseQuery.run(ThreadListenResponseQuery.java:46) at java.lang.Thread.run(Unknown Source)
Upvotes: 0
Views: 3892
Reputation: 23913
If you just suppress the entire exception will let really hard to figure out what is really going on. Instead I would just print a log output that could be something like this:
try {
// invocation that throws the exception
} catch (SocketException e) {
System.out.println(e.getMessage());
}
Upvotes: 2
Reputation: 1239
Surround socket.receive
with try-catch
:
try {
socket.receive()
} catch (SocketTimeoutException e) {
// Log an error, abort an communication, or just ignore
}
Upvotes: 2