在IntelliJ中,我正在研究一些从Google Cloud DataStore检索实体的代码,使用如下块:trycatch
try {
T dataAccessObject = class.newInstance();
entity = datastore.get(KeyFactory.createKey(dataAccessObject.GetEntityName().toString(), id));
dataModel = (T)dataAccessObject.ToModel(entity);
return dataModel;
} catch (EntityNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
对我来说,的空语句是一种代码异味,我宁愿删除它并允许抛出异常。catchEntityNotFoundException
但是,当我删除该 catch 语句时,它会导致编译器错误,并且我没有看到任何关于删除该语句无效的解释或理由。
正在调用实现接口的东西,这意味着可以抛出 一个,如果我们查看接口中定义的构造函数,可以看到这一点:datastore.getcom.google.appengine.api.datastore.DatastoreServiceEntityNotFoundException
com.google.appengine.api.datastore.DatastoreService
public interface DatastoreService extends BaseDatastoreService {
Entity get(Key var1) throws EntityNotFoundException;
但是,为什么我需要捕获异常?为什么我会收到编译错误?
慕田峪4524236
相关分类