Refactor untrapped exception reporting code