Factorize common code for exception handling