Remove duplication in exception handling