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

Towards Exception Safety Code Generation with Intermediate Representation Agents Framework

Xuanming Zhang, Yuxuan Chen, Yuan Yuan, Minlie Huang · DOI

Abstract

Large Language Models (LLMs) often struggle with robust exception handling in generated code, leading to fragile programs that are prone to runtime errors. We propose Seeker, a novel multi-agent framework that enforces exception safety in LLM generated code through an Intermediate Representation (IR) approach. Seeker decomposes exception handling into five specialized agents: Scanner, Detector, Predator, Ranker, and Handler that collaboratively analyze code, detect fragile segments, retrieve best practice exception strategies, and inject robust handling code. We also introduce Common Exception Enumeration (CEE), a comprehensive knowledge base derived from official documentation, technical practices, and real world code, to standardize exception handling strategies. Seeker also incorporates a Deep Retrieval-Augmented Generation (Deep RAG) algorithm to efficiently navigate the exception inheritance hierarchy, cutting down search overhead by 93% while improving accuracy in identifying relevant exceptions. We evaluate Seeker on 15 open source Java projects and multiple benchmarks. Seeker outperforms state of the art baselines, improving exception handling precision by up to 37% and overall code robustness by 38% as measured by expert code review. It significantly closes the gap between LLM and human developers in exception management, achieving a 28% success rate on real world issue fixes (SWE bench) versus 19% by prior methods. Our framework preserves functional correctness of code while proactively handling errors, demonstrating a practical, generalizable solution for safer code generation. In this paper, we discuss the novelty of using intermediate representation and multi-agent collaboration for exception handling, and outline how Seeker can be extended to other programming languages and complex software engineering tasks, aligning LLM-generated code with industrial standard.

Introduction

While Large Language Models (LLMs) have revolutionized automated code generation, they frequently stumble at the "last mile" of production-grade software: exception safety. This refers to a program's ability to handle unexpected runtime errors gracefully. Current LLM outputs often contain "fragile" segments that either ignore potential failures, implement overly generic "catch-all" blocks, or apply incorrect recovery strategies.

Human experts manage this complexity by navigating deep inheritance hierarchies and applying context-specific best practices. LLMs, however, lack this structured reasoning, resulting in a significant performance gap compared to senior developers. The Seeker framework addresses this by introducing a multi-agent approach that transforms the reasoning process into an interpretable intermediate representation.

The Seeker Framework

Seeker is built upon three core pillars designed to bridge the gap between functional code and robust, exception-safe software.

1. Multi-Agent Collaboration

Instead of a single monolithic prompt, Seeker utilizes five specialized agents to decompose the problem:

  • Scanner: Partitions the code into manageable units for analysis.
  • Detector: Identifies "sensitive" lines likely to throw errors, such as I/O or network calls.
  • Predator: Searches for specific exception types relevant to the detected fragile code.
  • Ranker: Evaluates and selects the optimal handling strategy (e.g., retry, log, or propagate).
  • Handler: Injects the final, robust code modifications into the source.

2. Common Exception Enumeration (CEE)

To standardize handling, we developed the CEE, a structured knowledge base derived from official documentation (like the JDK API) and enterprise style guides. It maps exceptions to typical scenarios and recommended handling logic, providing a ground truth for the agents.

3. Deep Retrieval-Augmented Generation (Deep-RAG)

Navigating Java’s 433+ exception classes is computationally expensive. Deep-RAG uses "scenario labels" to prune the search space, mimicking a human "branch-and-bound" approach to quickly identify relevant exception branches before pinpointing specific types.

A comparison between standard LLM code generation, which struggles with exception logic, and the Seeker framework, which uses intermediate representation and mapping to ensure exception safety.

Experimental Evaluation

We evaluated Seeker using 15 real-world open-source Java projects and the SWE-bench (2,294 GitHub issues).

  • Precision & Robustness: Seeker improved exception handling precision by 37% and overall code robustness by 38% over state-of-the-art baselines.
  • Efficiency: The Deep-RAG algorithm reduced search overhead by 93% while improving accuracy.
  • Real-World Impact: On the SWE-bench, Seeker achieved a 28% success rate, significantly outperforming the 19% achieved by prior agentic methods.
  • Expert Validation: In blind code reviews, Seeker’s output earned a 92% approval score, nearly matching human-written fixes.

Discussion and Key Insights

The core innovation of Seeker is the use of natural language dialogue between agents as an Intermediate Representation (IR) of reasoning. This makes the LLM's decision-making process interpretable and steerable. Furthermore, the transition from "coarse-grained reminding" to "fine-grained guiding" through step-by-step agent directives yields a linear improvement in code quality.

Conclusion and Future Work

Seeker demonstrates a practical, generalizable solution for safer code generation. While currently optimized for Java, the architecture is language-agnostic. Future work will focus on expanding the CEE knowledge base to languages like C++ and Rust and enabling the framework to automatically ingest project-specific exception hierarchies in private enterprise codebases.

Summary updated

BibTeX
@misc{zhang2024towards,
  author = {Xuanming Zhang and Yuxuan Chen and Yuan Yuan and Minlie Huang},
  title = {{Towards Exception Safety Code Generation with Intermediate Representation Agents Framework}},
  year = {2024},
  eprint = {2410.06949},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.06949},
}
Figure