NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Retrofitting Code Using LLMs to Support Exceptional Behavior

Linghan Zhong, Jiyang Zhang, Jayanth Srinivasa, Junyi Jessy Li, Milos GligoricarXiv 2026

TL;DR

This paper introduces the novel task of automatically retrofitting missing Exception Related Code (ERC)—such as throw statements and if guards—into existing software using Large Language Models guided by test suites. The authors present EXCODER, a framework that combines static and dynamic program analysis to engineer rich contextual prompts, boosting the performance of state-of-the-art LLMs by up to 13 percentage points on developer-written tests.

What Problem Does This Paper Address?

While modern LLMs excel at generating standard functional code, writing robust Exception Related Code (ERC) remains a tedious, manual chore for developers. ERC includes the conditions that detect bad states (e.g., checking if an input is null) and the throw statements or try/catch blocks that handle them.

Prior research on LLMs and exceptions has focused almost entirely on avoiding unhandled exceptions to keep programs from crashing. However, developers often want code to throw specific, meaningful exceptions when things go wrong. The authors address a major gap: how can we automatically inject the correct exception-handling logic into an existing codebase when given a set of Exceptional Behavior Tests (EBTs)? This effectively introduces a Test-Driven Development (TDD) workflow for exception handling.

Methods

The authors design and implement EXCODER (Exception Coder), a tool that acts as an intelligent context engineer for LLMs. Instead of just passing the raw code and tests to the model, EXCODER uses a two-pronged program analysis approach to gather crucial context:

  1. Static Analysis: It scans the repository to find available symbols (methods and fields) and exception constructors. This ensures the LLM knows exactly how to instantiate the correct exception type and what variables it can legally reference in its conditional checks.
  2. Dynamic Analysis: It executes the provided EBTs and monitors the runtime behavior. It captures line coverage (to pinpoint exactly where the exceptional state is triggered) and logs any intermediate exceptions already being thrown downstream. It also runs non-EBTs to ensure normal execution paths are preserved.

All of this information is woven directly into the code as inline comments before being fed to the LLM, giving the model a clear roadmap of where and how to insert the ERC.

Fig. 2: Overview of EXCODER.

Datasets

Because this is a brand-new task, the authors constructed a novel benchmark from real-world GitHub Java repositories:

  • The Core Dataset: They gathered 518 methods across 118 projects that originally contained developer-written exception code and matching EBTs.
  • The Evaluation Set: They systematically "stripped" out the ERC from 304 methods across 75 projects to create the evaluation benchmark.
  • Differential Testing: To rigorously verify semantic equivalence to the ground truth, they augmented the developer tests with thousands of automatically generated tests from popular tools like Randoop and EvoSuite.

Key Insights and Ideas

  • Static Context is King: In their ablation study, the authors discovered that providing static information—specifically available symbols and exception constructors—yielded the largest immediate performance spikes. It directly prevents the LLM from hallucinating API methods that don't exist.
  • Behavioral Context is Harder to Digest: Dynamic context like line coverage and thrown exceptions helped the models find the correct solution over multiple samples, but models struggled to utilize it consistently on every single attempt. This highlights a need for better prompting strategies when dealing with abstract runtime behavior.
  • The "Too Lenient" Pitfall: A significant portion of LLM errors were "false positives" where the generated code passed all user tests but was still incorrect. The most common issue was being too lenient (e.g., checking if a string is null, but forgetting to check if it's empty), usually because the developer's test suite wasn't comprehensive enough to specify the exact boundary conditions.

Key Results

EXCODER was evaluated across five prominent LLMs (Llama3.1 8b, Phi4 14b, Qwen 2.5 Coder 7b/32b, and GPT-5 Mini) and consistently outperformed baseline prompts across all metrics.

  • Qwen 2.5 Coder 32b: Reached a 85.92% pass@1 rate, a massive 12.56 percentage point increase over the baseline.
  • GPT-5 Mini: Achieved a stellar 95.03% pass@1 rate (an 12.30 p.p. boost).
  • Synergy with Self-Repair: When combined with an iterative self-repair loop (where compiler/test feedback is fed back to the model), Iter-EXCODER pushed Qwen 32b's performance to 96.22% on pass@5.
Fig. 3: Overlap between target methods successfully handled by the baseline versus EXCODER. Each circle covers the target methods for which at least one of the 10 generated samples meets the criterion.

Limitations and Future Work

While highly effective, EXCODER currently operates under a few constraints:

  • Direct Exceptions Only: It only targets exceptions that originate directly within the method under test. It does not handle complex scenarios where exceptions propagate through deep call chains across multiple classes.
  • Test Dependency: The framework is fundamentally bounded by the quality of the EBTs. If a test suite is underspecified, the LLM will generate structurally valid but logically incomplete exception guards.

Future work will focus on extending the tool to handle multi-method exception propagation and developing techniques to help developers write more descriptive, complete exceptional behavior tests.

Summary updated

BibTeX
@misc{zhong2026retrofitting,
  author = {Linghan Zhong and Jiyang Zhang and Jayanth Srinivasa and Junyi Jessy Li and Milos Gligoric},
  title = {{Retrofitting Code Using LLMs to Support Exceptional Behavior}},
  year = {2026},
  eprint = {2609.10397},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2609.10397},
}
Figure