An Empirical Evaluation of Using Large Language Models to Recommend Exception Handling Tasks
Large Language Models (LLMs) are increasingly used to support software engineering tasks such as code generation, bug fixing, and program comprehension. However, their ability to support exception handling tasks remains largely unexplored. Prior work on automated exception handling has mostly relied on task-specific models, fine-tuning, or specialized pipelines, which may limit reuse across projects and as APIs evolve. In this paper, we empirically evaluate whether open-source and proprietary LLMs can support Python exception handling tasks through prompting alone, without fine-tuning. We study four tasks: (1) detecting whether exception handling is necessary, (2) localizing statements that should be enclosed in a try block, (3) recommending exception types for except clauses, and (4) generating exception handlers. We further complement the generation task with a quality assessment of the generated exception handlers. We evaluated several LLMs, including CodeLlama, Phi-4, GPT-4 Mini, Claude Haiku 4.5, and Gemini 3 Flash, across four prompting strategies using a benchmark from 21 open-source Python projects, collecting 479,345 functions and curating 379 code snippets. Results showed stronger effectiveness for exception necessity detection and try-block localization, with best F-scores of 0.752 and 0.733, respectively, while exception-type recommendation and handler generation remained substantially more challenging. Overall, our findings indicate that prompt-based LLMs are more suitable as early-stage exception handling tasks for detecting need contexts and localizing risky statements. Also, generated exception handlers still require validation, as they often diverge semantically from developer-written implementations and introduce more exception-related quality violations.