有返回值的函数,如果包含try/catch块,经常会出现没有return的错误

public static Connection getConnection(){
try {
return datasource.getConnection();
} catch (SQLException e) {
//这里得用运行时异常。不然就会报没有return的错误
System.out.println(e);
throw new RuntimeException(e);//一定要加上这句,因为运行时异常不可以抛出去,当try中出现异常程序员就必须得修改代码,因此这样代码一定就会有return。
}
}

posted @ 2017-06-22 17:51  wells163  阅读(404)  评论(0编辑  收藏  举报