java - Is it possible for try-with-resource to fail to close resources? -


as per oracle documentation

the try-with-resources statement try statement declares 1 or more resources. resource object must closed after program finished it. try-with-resources statement ensures each resource closed @ end of statement. object implements java.lang.autocloseable, includes objects implement java.io.closeable, can used resource

if resource not implement autocloseable cannot declared within try block, has declared in body part & has explicitly closed in block. possible try-with-resource fail clean resources unless use idiom incorrectly if there nested resources & close() call not idempotent underlying resource? understand try-with-resource ensures close() getting called behind scene, if close not close underlying resources try-with-resource remain ineffective clean-up resources. have contrary view or more clarification?

disregarding code not work (like example of close() methods not closing resource) or such external factors people attaching debuggers side-step cleanup, can think of 3 situations in try-with-resources might fail clean resource:

  • power failure, hardware failure, os crash or vm crash. there isn't software can such things, outside scope of resource handling. however, still need keep in mind in case you're thinking managing such things mandatory locking files , whatnot try-with-resources statement.
  • asynchronous exceptions, such generated thread.stop(). if thread "stopped" in way while in clean-up code, clean-up code aborted. unsafety of such asynchronous exceptions why thread.stop() deprecated , replaced thread.interrupt(), safe.
  • if system.exit() called. might considered of above cases, except might happen under "normal circumstances".

in other respects, should able rest assured close() called. try-with-several-resources same nested try blocks, clean-up code throwing exceptions won't stop other clean-up routines.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -