Fix exception handling for reading/writing