Java方法优化:查找并修复错误与冗余代码
{"title":"public FlightOrder searchOrder(int oid) {\n\ttry {\n\t FlightOrder forder = new FlightOrderDaoImpl().findByOid(oid);\n\t return forder;\n\t} catch (SQLException e) {\n\t e.printStackTrace();\n\t throw new RuntimeException(e);\n\t}\n\treturn null;\n}\n题目:\n1、有一处报错的地方,请找出,并说明报错原因\n\n\n2、作为service方法,它有一处编写不恰当的地方,请找出\n\n\n内容:1、报错的地方是在catch块中的throw语句,因为在catch块中已经处理了SQLException异常并抛出了RuntimeException异常,所以不需要再返回null。\n\n\n2、不恰当的地方是在方法的最后一行返回了null。在try块中已经有返回语句,所以不需要在最后再返回null。可以直接将方法的最后一行删除。"}
原文地址: https://www.cveoy.top/t/topic/p6SI 著作权归作者所有。请勿转载和采集!