Discussion of Java-Try-With-Resources

Well, now, at my work, we are discussing resource-attempts and suppressing exceptions.

Quick tip: try-with-resources, from java 7, eliminates the need for this annoying finally block to close resources. I personally think this is more elegant, but I have a colleague who is not convinced. He does not like that one exception is suppressed, and he claims that we are losing information through this.

At first I took him for it, because I am a younger maiden, and he is old, I am a beginner, etc. But recently, I discovered that hey, all the information falls into the stack trace, including the thrown exception. Thus, information is not lost.

The last part of this discussion that I'm looking for (since I advocate trying to use resources) is how auto-close handles this exception. Let them say that when you try to close the resource, an exception occurs, is there a potential that the resource can remain open and flow? Ultimately, this may not be so important, as the magazines will warn us about this issue anyway.

Just curious. Thank you very much.

+5
source share
1 answer

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.

+8
source

Source: https://habr.com/ru/post/1247976/


All Articles