Slightly refactored exception handler.