Ensure file handles are closed when exception occurs in lines 206 - 217

This commit broadens the try/finally as previously it was possible that file handles weren't closed if an exception were to occur in lines 206 - 217 (i.e., during and after creating the file handles but before entering the try/finally that later ensures `.close()` is called on the file handles).