Best practices for exception in my DAO layer

I’m working on my dao layer implementation and I’m confused about the exception handling part. I have many solution to throw exception so what is the best solution to handle exception in dao layer :

This is my code :

My DAO Interface :

@Repository
@Transactional
public interface GnqstRepository<T,V>  {

   public  T save(T t);
   public  void delete(V v)throws GnqstRepositoryNotFoundException;
   public  T update(T t); 
   public  T find(V v)throws GnqstRepositoryNotFoundException;
   public  List<T> findAll();


}

And this is my this an implementation for an entity :

public class EnqueteurRepository implements GnqstRepository<Enqueteur, Long> {
    @PersistenceContext(name="gnst")
    private EntityManager em ;

    private static final Logger logger = Logger.getLogger(EnqueteurRepository.class);


    @Override
    public Enqueteur find(Long v)throws GnqstRepositoryNotFoundException {
        Enqueteur enqueteur = em.find(Enqueteur.class,v);
        if(enqueteur!= null){
            logger.info("EnqueteurRepository : l'enqueteur avec l'ID : "+v+" a été trouvé avec succès.");
            return enqueteur;
        }
        else{
            throw new GnqstRepositoryNotFoundException("L'identifiant entré ne ressemble pas à un ID enqueteur valide.");
        }
    }
}

Here I create a checked exception my question is there best way than this to throw an exception to the next layer in case for example the find method do not find an entity ?

My objectif is that the user in IHM when click in button find for search in case of no result found then a message should show in IHM in my case I throw exception with a message throught dao layer,business layer to presentation layer my question is who is the best throw an exception or return null to the presentation layer then check an return message to the user ?Thank in advance

Answer

There are no hard and fast rules regarding exception handling, but these are my recommendations:

You have to ask yourself what the users of your repository would like to do when they do a lookup with a missing id.

If they want to take immediate action – for instance use a fallback repo – I would simply return a value representing “miss”, such as null or Optional.empty().

If you want the error to propagate several layers up your application and maybe render a 404 or abort the running program, I would use an exception.

If you don’t want to handle exceptions from different repositories differently, I would just stick with the standard EntityNotFoundException. If you do want to handle them differently I would introduce my own RuntimeExceptions.

Checked exceptions are quite rarely used today. They exist in the borderland between returning values representing failures and throwing exceptions. I believe that the consensus in the community is that they sound like a good idea but are bad in practice. So I would avoid them.

Leave a Reply

Your email address will not be published. Required fields are marked *