ElasticSearch Exception Handling

I want to throw an exception when the elasticsearch client is not connected to its server or whenever it is not able to index the data to the server.