exception handling for reading and writing files