Does the termination of a program effectively clean up resources or it is still needed to be done explicitly? I would get rid of the boilerplate if it has no benefit.
I understand the need in case of long-lived program where the resource usage is shorter than the lifecycle of the program. But what is the case when the resource (like a singleton HTTP client) is used until the end?
1. No explicit cleanup
Does this version clean things up when the program exits?
public static void main(String[] args) {
CloseableHttpClient client = HttpClients.createDefault();
// execute requests
}
2. Cleanup using try with resources
This version obviously does clean up but requires additional code.
public static void main(String[] args) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
// execute requests
} catch (IOException e) {
// exception handling
}
}