[minor] Tweaks to serialization logic to ensure errors are reported.
Previous implementation could mean that for example for permissions
errors, the file handle would not initialize properly and then the close
operation in the finally clause would actually result in an error rather
than closing the file.