In IntelliJ I'm working on some code that retrieves entities from Google Cloud DataStore, using a try
catch
block like this:
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) {
}
To me, the empty catch
statement for the EntityNotFoundException
is a code smell, and I'd rather remove it and allow the exception to be thrown.
When I remove that catch statement however, it causes a compiler error, and I'm not seeing any explanation or rationale as to why removing the statement is invalid.
The datastore.get
is calling something that implements the com.google.appengine.api.datastore.DatastoreService
interface, which means it's possible for an EntityNotFoundException
to be thrown, which can be seen if we look at the constructor as defined in the interface:
com.google.appengine.api.datastore.DatastoreService
public interface DatastoreService extends BaseDatastoreService {
Entity get(Key var1) throws EntityNotFoundException;
Why would I need to catch the exception though? Why do I get the compile error?