Journal of Frontiers of Computer Science and Technology ›› 2023, Vol. 17 ›› Issue (3): 667-677.DOI: 10.3778/j.issn.1673-9418.2107042

• Artificial Intelligence·Pattern Recognition • Previous Articles     Next Articles

Intelligent Code Recommendation for Exception Handling

LIN Kai, TAO Chuanqi, HUANG Zhiqiu   

  1. 1. College of Computer Science and Technology, Nanjing University of Aeronautics and Astronautics, Nanjing 211106, China
    2. Ministry Key Laboratory for Safety-Critical Software Development and Verification, Nanjing University of Aeronautics and Astronautics, Nanjing 211106, China
    3. State Key Laboratory for Novel Software Technology, Nanjing University, Nanjing 210023, China
    4. Collaborative Innovation Center of Novel Software Technology and Industrialization, Nanjing 210093, China
  • Online:2023-03-01 Published:2023-03-01

面向异常处理的代码智能化推荐

林锴,陶传奇,黄志球   

  1. 1. 南京航空航天大学 计算机科学与技术学院,南京 211106
    2. 南京航空航天大学 高安全系统的软件开发与验证技术工信部重点实验室,南京 211106
    3. 南京大学 计算机软件新技术国家重点实验室,南京 210023
    4. 软件新技术与产业化协同创新中心,南京 210093

Abstract: Exception handling is widely used in program development to ensure software robustness and system reliability. Users want to select the right exception handling code to prevent program crashes or memory leaks. However, it is difficult for users, especially novices, to write proper exception handling code due to lack of experience. In order to assist developers to write correct exception handling code, this paper proposes DeepEHCR, an intelligent code recommendation approach for exception handling. It constructs API call tree to represent the context code information of the location where the exception occurs, and uses the self-attention network to recommend the exception handling strategy that the context code needs to take. For the strategy of HANDLE, the Transformer model is employed to further recommend related API sequence to handle exceptions. Experimental results show that DeepEHCR has high performance in both exception handling strategy recommendation and API sequence recommendation. In terms of exception handling strategy recommendation, the values of Accuracy, Precision, Recall and F1-score reach 89.78%, 89.98%, 89.34% and 89.59%, respectively. In terms of API sequence recommendation, the values of Hit@1/3/5 reach 57.83%, 69.73% and 74.79%, respectively. In addition, DeepEHCR outperforms baselines on fixing real exception bugs.

Key words: exception handling, code recommendation, self-attention network, context code

摘要: 为了保证软件的健壮性以及系统的可靠性,异常处理被广泛应用于软件开发过程中。开发者们希望选择合适的异常处理代码以防止出现程序崩溃或者内存泄漏等问题。然而开发者,尤其是新手,由于缺乏经验很难编写出正确或规范的异常处理代码。为了协助开发者编写正确的异常处理代码,提出了面向异常处理的代码智能化推荐方法,即DeepEHCR。该方法通过构建API调用树来表示异常发生位置的上下文信息,然后根据上下文信息,利用自注意力网络推荐相应的异常处理策略。针对处理异常这一具体策略,利用Transformer模型进一步推荐处理异常相关的API序列。实验表明DeepEHCR在异常处理策略推荐和API序列推荐上都有较高的性能。在异常处理策略推荐方面,Accuracy、Precision、Recall以及F1-score的值分别达到了89.78%、89.98%、89.34%以及89.59%。在API序列推荐方面,Hit@1/3/5的值分别达到了57.83%、69.73%、74.79%。除此之外,DeepEHCR在修复真实的异常漏洞方面也优于现有研究工作。

关键词: 异常处理, 代码推荐, 自注意力网络, 上下文代码