Modified code for handling exceptions.