Revert "Replace try and finally block with try-with-resources"

This reverts commit b3cb9ff351ed905a14c8051a046e5be4b5204b95.