I see some Spring/Hibernate code having different policies regarding declaring DataAccessException in DAO interface methods.
Some do explicitly declare it and some don’t (or only from time to time):
public interface FlightDao {
boolean decrementSeat(Long flightId, int quantity);
List<Flight> findFlights(String fromAirportCode, String toAirportCode) throws DataAccessException;
public List<Flight> getFlights();
Flight getFlight(Long id);
Flight getFlight(String flightNumber);
void save(Flight flight);
}
What would be considered the best practice and why?
Update
From section 13.2.2 of the spring tutorial, it is important to annotate the implementing DAO pojo with @Repository to make sure the underlying ORM (or JDBC) exceptions are automatically translated into the DataAccessException (runtime) exception hierarchy.
As you can see here, this is a RuntimeException, so wether you declare it or not makes no difference in terms of programming. When the user implements the method he can choose to omit this exception from the method signature.
The only reason i can think of for putting it in the method signature is to mark it so the user will know this method might throw this exception and thus he can choose wether to catch it and handle it.