title: How to handle/surpress Java ElasticSearch ConnectTransportException
body: <p>I am currently setting up a small project using elastic search in Java, and I am trying to figure out how to gracefully handle the 'ConnectTransportException' when my ElasticSearchCluster is down.</p>
  
  <pre><code>private TransportClient buildClient() {
      TransportClient client = null;
      try {
          client = new PreBuiltTransportClient(Settings.EMPTY)
              .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
      } catch(UnknownHostException ex) {               
          logger.error("Unknown host {}: {}", "localhost", ex.getMessage());
      }
      return client;
  }
  </code></pre>
  
  <p>This code works fine. When my ElasticSearch cluster isn't running I want to be able to know if the client was not able to connect to it and log an appropriate message and then handle it appropriately.</p>
  
  <p>Currently when the client can't connect to the cluster I get this error: </p>
  
  <pre><code>org.elasticsearch.transport.ConnectTransportException: [][127.0.0.1:9300] connect_timeout[30s]
      at org.elasticsearch.transport.netty4.Netty4Transport.connectToChannels(Netty4Transport.java:370) ~[transport-netty4-client-5.3.0.jar:5.3.0]
      at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:495) ~[elasticsearch-5.3.0.jar:5.3.0]
      at org.elasticsearch.transport.TcpTransport.connectToNode(TcpTransport.java:460) ~[elasticsearch-5.3.0.jar:5.3.0]
      at org.elasticsearch.transport.TransportService.connectToNode(TransportService.java:314) ~[elasticsearch-5.3.0.jar:5.3.0]
      at org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.doSample(TransportClientNodesService.java:408) [elasticsearch-5.3.0.jar:5.3.0]
      at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.sample(TransportClientNodesService.java:354) [elasticsearch-5.3.0.jar:5.3.0]
      at org.elasticsearch.client.transport.TransportClientNodesService.addTransportAddresses(TransportClientNodesService.java:195) [elasticsearch-5.3.0.jar:5.3.0]
      at org.elasticsearch.client.transport.TransportClient.addTransportAddress(TransportClient.java:322) [elasticsearch-5.3.0.jar:5.3.0]
      at elk.aggregator.ElasticClient.buildClient(ElasticClient.java:72) [classes/:?]
      at elk.aggregator.ElasticClient.&lt;init&gt;(ElasticClient.java:62) [classes/:?]
      at elk.aggregator.ElasticClient.getInstance(ElasticClient.java:55) [classes/:?]
      at elk.aggregator.App.main(App.java:9) [classes/:?]
  Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:9300
      at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[?:1.8.0_111]
      at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[?:1.8.0_111]
      at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:346) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
      at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
      at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:630) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
      at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:527) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
      at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:481) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
      at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:441) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
      at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) ~[netty-common-4.1.7.Final.jar:4.1.7.Final]
      at java.lang.Thread.run(Thread.java:745) ~[?:1.8.0_111]
  </code></pre>
  
  <p>I want to be able to handle this gracefully by catching the error, but simply adding another catch block catching ConnectTransportException does not work. </p>
  
  <p>Any idea how to do this?</p>