简体   繁体   中英

AutoCloseable and throws exception

The AutoCloseable has a method 'void close throws Exception'. Some suggest to extend this interface and override the close method and remove the Exception. See for instance the below link.

But when i forget to put the statement in a try-with-resources statement is the close method than not called?

  • In the try-with-resources the close is called
  • In the stream api the close is called
  • Automatically by java when for instance leaving the method in which the resource is created???

Additional information I've searched on the internet and looked at the documentation. Both suggested to remove the throws Exception statement. But my reaction to this suggestion is that when in the future someone in our project implements the new interface (without exception) and forgets to put them in the try-with-resource that we have a resource leak. For our project it's not a huge issue because we're using SonarQube which will mention the resource leak

Example location:

The AutoCloseable has a method 'void close throws Exception'. Some suggest to extend this interface and override the close method and remove the Exception.

The link you posted isn't suggesting that as a general case, it's showing you why the io package has its own Closeable interface that only throws an IOException (because it doesn't need to throw a more general Exception .) This makes zero difference at runtime, it's purely in place so a compiler error is thrown if anything tries to throw something that's not an IOException while using the io.Closeable interface.

But when i forget to put the statement in a try-with-resources statement is the close method than not called?

You seem to be implying a link here where there is none - the type of exception that an AutoCloseable might throw isn't really anything to do with when the resource it wraps is closed.

In the try-with-resources the close is called

Yes, which is the whole point of the language feature.

In the stream api the close is called

Not unless you wrap it in a try with resources block.

Automatically by java when for instance leaving the method in which the resource is created???

Nope, if you forget to close a resource it'll just stay open.

(Only exception to this rule is if a resource closes itself in a finalizer, which executes just before it's garbage collected. This certainly isn't something to rely on though, it's just (sometimes) used as a fallback.)

Try-with-resources automatically calls close when the resource goes out of scope (also: you can only declare an AutoClosable in try-with-resources). If you forget to put your resource in that language construct, you have to close it manually.

The AutoCloseable.close() method is not a destructor (there are no destructors in Java) - it was specifically created for try-with-resources and therefore is not called if not used in that statement - see documentation :

The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM