How can I eliminate connection exception in Java?

I want to know how I can catch the "connection excluded" exception in Java when I use socket. (what happens if the server is down or not responding).

Below is how I implemented so far.

try { sockfd = new Socket(host.getHostName(),heart_port); sockfd.setReuseAddress(true); BufferedReader message = new BufferedReader(new InputStreamReader ( sockfd.getInputStream() ) ); message.close(); sockfd.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+4
source share
1 answer

Add ConnectException before IOException

 catch (ConnectException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+13
source

All Articles