Refactoring in favor of try-with-resources, added a new Exception.