一句话:提出首个异常抛出推荐方法 ThEx,基于源码上下文学习分类模型预测应抛出的异常。
异常处理机制对解决运行时错误至关重要,但若抛出的异常本身不正确,后续捕获和处理便无法正确进行,并已导致真实严重缺陷。现有研究多关注捕获、处理异常及检测相关 bug,却没有方法推荐应抛出哪个异常。异常在程序中广泛使用,却常缺乏文档且含义模糊,不同项目团队的异常使用习惯也可能完全不同,因此即使经验丰富的程序员也难以判断该抛何异常。本文提出首个方法 ThEx,从源文件中已有的抛出异常学习分类模型,提取抛出位置、相关变量名等异常周围代码信息作为特征;给定新的编程上下文,ThEx 可预测其最合适的异常。
Although the exception handling 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 predict its best exception(s).