I\'m using a third party Java library to interact with a REST API. The REST API can sometimes take a long time to respond, eventually resulting in a java.net.ConnectExcept
java.net.ConnectExcept
static final int NUM_TRIES =4; int tried =0; boolean result =false; while (tried < NUM_TRIES && !result) { try { Entity entity = new Entity(); result = entity.methodThatMakesUseOfRestApi(); } catch () { if ( tried == NUM_TRIES) { throw new TimeoutException(); } } tried++; Thread.sleep(4000); }