# How to handle Host and Port Cases, when we creat a Client API

**URL:** <https://discuss.elastic.co/t/how-to-handle-host-and-port-cases-when-we-creat-a-client-api/188871>\
**Category:** Elasticsearch\
**Created:** [July 4, 2019, 9:18am UTC](https://discuss.elastic.co/t/how-to-handle-host-and-port-cases-when-we-creat-a-client-api/188871 "2019-07-04T09:18:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)\
**Post date:** [July 4, 2019, 9:18am UTC](https://discuss.elastic.co/t/how-to-handle-host-and-port-cases-when-we-creat-a-client-api/188871/1 "2019-07-04T09:18:05Z")

</div>

Hello folks,  
i am trying to creat Connection to Elasticsearch using Client API, and i want to handle Cases , if the Host is not existing or unknown or the Port ist wrong or not existing or empty,  
how to write that in java ,  
here is my Start,

```auto
try {
 client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress
                        .getByName("localhost"), 9300));
} catch (UnknownHostException ex) {                               
            System.out.println("faild host " + ex.getMessage());
            client.close();
        }catch(UnknownTransportNummber nr){
            System.out.println("faild port" + nr.getMessage());
}

```

i want to check and handle the localhost , and PortNummber, is there any tutorial or Advice how to do that??  
I found "UnknownHostException" to handle the Host error , is there any Exception to handle the Port error something like "UnknownTransportNummber " or "UnknownPort" ??  
thx

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 4, 2019, 1:32pm UTC](https://discuss.elastic.co/t/how-to-handle-host-and-port-cases-when-we-creat-a-client-api/188871/2 "2019-07-04T13:32:33Z")

</div>

Not sure. But anyway don't use the TransportClient as it's removed in the coming 8.0 version.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 1, 2019, 1:33pm UTC](https://discuss.elastic.co/t/how-to-handle-host-and-port-cases-when-we-creat-a-client-api/188871/3 "2019-08-01T13:33:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
