# From Misuse to Mastery: Enhancing Code Generation with Knowledge-Driven AI Chaining

arXiv: https://arxiv.org/abs/2309.15606 · HTML: https://mlsanity.com/paper/2309.15606/

## Key facts

- Cited by 16 papers in the map (data through 14 Oct 2026).
- First posted on arXiv 27 Sep 2023.

## Built on by (most cited first, 16 of 16)

- [Large Language Model-Based Agents for Software Engineering: A Survey](https://mlsanity.com/paper/2409.02977/) (2024)
- [Agents in Software Engineering: Survey, Landscape, and Vision](https://mlsanity.com/paper/2409.09030/) (2024)
- [A Survey on Data Synthesis and Augmentation for Large Language Models](https://mlsanity.com/paper/2410.12896/) (2024)
- [Agent READMEs: An Empirical Study of Context Files for Agentic Coding](https://mlsanity.com/paper/2511.12884/) (2025)
- [A Pair Programming Framework for Code Generation via Multi-Plan Exploration and Feedback-Driven Refinement](https://mlsanity.com/paper/2409.05001/) (2024)
- [Preference-Guided Refactored Tuning for Retrieval Augmented Code Generation](https://mlsanity.com/paper/2409.15895/) (2024)
- [Large Language Models for Software Engineering: A Reproducibility Crisis](https://mlsanity.com/paper/2512.00651/) (2025)
- [Towards Exception Safety Code Generation with Intermediate Representation Agents Framework](https://mlsanity.com/paper/2410.06949/) (2024)
- [EvoSci: A Bio-Inspired Multi-Agent Framework for the Evolution of Scientific Discovery](https://mlsanity.com/paper/2605.24018/) (2026)
- [Exploring the Integration of Large Language Models in Industrial Test Maintenance Processes](https://mlsanity.com/paper/2409.06416/) (2024)
- [Seeker: Towards Exception Safety Code Generation with Intermediate Language Agents Framework](https://mlsanity.com/paper/2412.11713/) (2024)
- [VibeCodeHPC: An Agent-Based Iterative Prompting Auto-Tuner for HPC Code Generation Using LLMs](https://mlsanity.com/paper/2510.00031/) (2025)
- [Explore-Construct-Filter: An Automated Framework for Rich and Reliable API Knowledge Graph Construction](https://mlsanity.com/paper/2502.13412/) (2025)
- [Designing Empirical Studies on LLM-Based Code Generation: Towards a Reference Framework](https://mlsanity.com/paper/2510.03862/) (2025)
- [Prompt Coach: An Empirical Evaluation of an Agentic Tutor for Learning Prompt Engineering in Software Development](https://mlsanity.com/paper/2607.06074/) (2026)
- [Retrofitting Code Using LLMs to Support Exceptional Behavior](https://mlsanity.com/paper/2609.10397/) (2026)

## Abstract (arXiv)

Large Language Models (LLMs) have shown promising results in automatic code generation by improving coding efficiency to a certain extent. However, generating high-quality and reliable code remains a formidable task because of LLMs' lack of good programming practice, especially in exception handling. In this paper, we first conduct an empirical study and summarise three crucial challenges of LLMs in exception handling, i.e., incomplete exception handling, incorrect exception handling and abuse of try-catch. We then try prompts with different granularities to address such challenges, finding fine-grained knowledge-driven prompts works best. Based on our empirical study, we propose a novel Knowledge-driven Prompt Chaining-based code generation approach, name KPC, which decomposes code generation into an AI chain with iterative check-rewrite steps and chains fine-grained knowledge-driven prompts to assist LLMs in considering exception-handling specifications. We evaluate our KPC-based approach with 3,079 code generation tasks extracted from the Java official API documentation. Extensive experimental results demonstrate that the KPC-based approach has considerable potential to ameliorate the quality of code generated by LLMs. It achieves this through proficiently managing exceptions and obtaining remarkable enhancements of 109.86% and 578.57% with static evaluation methods, as well as a reduction of 18 runtime bugs in the sampled dataset with dynamic validation.
