Slight cleanup of error code.

Moved closure functions into exception block to make it clear that they are to do
with exception handling