Rework exception handling

Reduce code duplication.

Change-Id: I3d02578f9c95cb44809b439c506bdc29695cfc19