Updated code to use appropriate exceptions.