Refactored exception handler