You are right that suppressing an exception does not result in loss of information. Your colleague on this subject is unfounded. The try-with-resources points are:
to make sure that the resources are closed, regardless of what is thrown when they are used, and in the reverse order in which they are declared,
so that the exceptions that were thrown at closing do not throw exceptions that were thrown into the try block to get lost, and
to make sure that exceptions thrown at the closure are still saved as excluded exceptions, so information is not lost.
If closing a resource throws an exception, then you can do nothing about it. In JDBC, database objects access the database server to inform it of resource cancellation, and they will remain open if the failure is complete. (And retrying, as a rule, is pointless, because the problem, as a rule, is that something has changed on the network or in the connection.) But the server will clear them ultimately, and this is from the clientβs hands. Both try-with-resources and the older hieroglyphs of try-finally do equally good job of closing resources, it is just more work and more typing with the try-finally idiom.
The biggest difference for me is whether to use try-with-resources or nested try-finally statements, is that in the first case, if nothing is thrown in the try block and something throws to close, the exception throws at closing (since there were no exceptions in the try block to attach it as an excluded exception). If you use nested try-finally blocks, you can make sure that exceptions that were closed are not propagated from the finally block, so that an intermittent network failure when releasing resources does not result in the loss of a valid business transaction.
But in practice, very few people tolerate this kind of nesting, and they use shortcuts that lead to resource leaks (usually these are connections that do not close because the previous call in the finally block failed). People also tend to write code that causes masking of exceptions (mentioned at the second point of the token), where an exception that is thrown when it closes causes an exception that is excluded from the try block; try-with-resources prevents such an error. There is definitely a place for trial resources.
My advice is to study everything you can know about exception handling, write examples of programs that demonstrate how exceptions work, and understand the strengths and weaknesses of both approaches so that you can talk in detail about them, compare and compare. In this way, you can show that you understand the problems that your colleagues are raising, and you can give advice not as proponents of one path, but to help your group find solutions that will help them write better software.
source share