I am using SonarQube for code quality. I got one issue related to exception handling, which says remove throw clause from finally block.
} catch(Exception e) {
throw new MyException("request failed : ", e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
throw new MyException("failed to close server conn: ", e);
}
}
Based on my understanding above code looks good. If I remove throw clause and suppress exception in finally then caller of this method will not be able to know server's status. I am not sure how we can achieve same functionality without having throw clause.
finally
block in that innertry-catch
block and see if it still warns? – Aghastfinally
block still throws yourMyException
, am I right? – Blackdampif
statement before the throw clause, so that it only throws the exception if the connection was ever made in the first place? – Blackdamp