Which Exception Shall We Throw?
Hao Zhong
Abstract
With the support of exception handling mechanisms, when an error occurs, its corresponding typed exception can be thrown. A thrown exception can be caught and the handling code will resolve the error (e.g., closing resources), if the type of the thrown exception matches the type of the expected exceptions. Although this mechanism is critical for resolving runtime errors, bugs inside this process can have far-reaching impacts. Therefore, researchers have proposed various approaches to assist catching and handling such thrown exceptions and to detect corresponding bugs. If the thrown exceptions themselves are incorrect, their errors will never be correctly caught and handled. Like bugs in catching and handling exceptions, wrong thrown exceptions have caused real critical bugs. However, to the best of our knowledge, no approach has been proposed to recommend which exceptions shall be thrown. Exceptions are widely adopted in programs, often poorly documented, and sometimes ambiguous, making the rules of throwing correct exceptions rather complicated. A project team can leverage exceptions in a way totally different from other teams. As a result, even experienced programmers can have difficulties in determining which exception shall be thrown, although they have the skills to implement its surrounding code. In this paper, we propose the first approach, ThEx, to predict which exception(s) shall be thrown under a given programming context. The basic idea is to learn a classification model from existing thrown exceptions in source files. Here, the learning features are extracted from various code information surrounding the thrown exceptions, such as the thrown locations and related variable names. Then, given a new context, ThEx can automatically predict its best exception(s). We have evaluated ThEx on 12,012 thrown exceptions that were collected from nine popular open-source projects. Our results show that it can achieve high f-scores and mcc values (both around 0.8). On this benchmark, we also evaluated the impacts of our underlying technical details. Furthermore, we evaluated our approach in the wild, and used ThEx to detect anomalies from the latest versions of the nine projects. In this way, we found 20 anomalies, and reported them as bugs to their issue trackers. Among them, 18 were confirmed, and 13 have already been fixed.
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Your agent calls
Luneget_paper_fulltext
Free to start. No credit card required.
Terminal
Install the CLIlune papers fulltext 010bb4eb-8701-45e5-8803-ac12d4caa068Cited by top-tier papers3
- exLong: Generating Exceptional Behavior Tests with Large Language ModelsJiyang Zhang, Yu Liu, Pengyu Nie, Junyi Jessy Li et al.ICSE 2025 · 2 citations
- Cut to the Chase: An Error-Oriented Approach to Detect Error-Handling BugsHaoran Liu, Zhouyang Jia, Shanshan Li, Yan Lei et al.FSE 2024 · 1 citation
- Error Delayed Is Not Error Handled: Understanding and Fixing Propagated Error-Handling BugsHaoran Liu, Shan-shan Li, Zhouyang Jia, Yuanliang Zhang et al.FSE 2025
Builds on2
Related papers
- Learning to Handle ExceptionsJian Zhang, Xu Wang, Hongyu Zhang, Hailong Sun et al.ASE 2020 · 19 citations
- Programming Assistant for Exception Handling with CodeBERTYuchen Cai, Aashish Yadavally, Abhishek Mishra, Genesis Montejo et al.ICSE 2024 · 4 citations
- BERT-Based Code Learning for Exception Localization and Type PredictionChongyu Zhang, Qiping Tao, Liangyu Chen, Min ZhangAAAI 2025 · 1 citation
- Detecting Exception Handling Bugs in C++ ProgramsHao Zhang, Ji Luo, Mengze Hu, Jun Yan et al.ICSE 2023 · 7 citations
- Reducing Bug Triaging Confusion by Learning from Mistakes with a Bug Tossing Knowledge GraphYanqi Su, Zhenchang Xing, Xin Peng, Xin Xia et al.ASE 2021 · 19 citations
