Add a helper function for catching exceptions and retries.