[CA-119964] Trap exceptions and recover gracefully

Signed-off-by: Ben Chalmers <Ben.Chalmers@citrix.com>