title: Best practise - exception handling in service/dao/business layers
body: <p>What is the best practise to do exception handling when error is thrown in different layers.</p>
  
  <p>I have 4 layers of code - DAO , SERVICE , BUSINESS, PRESENTATION. I am trying to catch some run time exception in the dao layer and want it to display in the presentation layer with some message.Is the below approach a good one ? </p>
  
  <p>Here in the code snippets  - DataException is my runtime exception class.Service and Business exception classes are my checked exception implementation classes.</p>
  
  <p>Code snippets below:</p>
  
  <p>In dao layer , a method checks for some value from database</p>
  
  <pre><code>class dao{
  public User getUser() throws DataException{
  
  User user = null;
  
  try
  {
  //some operation to fetch data using hibernatetemplate
  }catch(Exception ex){
  throw new DataException(ex.getMessage());
  }
  
  return user;
   }
   }
  </code></pre>
  
  <p>service.java</p>
  
  <pre><code> class service{
   public User getUser(String username) throws ServiceException{
  
   User user = null;
  
   try
  {
  //some operation to fetch data using dao method
   dao.getuser(username);
   }catch(DataException ex){
  throw new ServiceException(ex.getMessage());
  }
  
   return user;
  }
  }
  </code></pre>
  
  <p>business.java</p>
  
  <pre><code> class business{
   public User getUser(String username) throws BusinessException{
  
   User user = null;
  
   try
  {
  //some operation to fetch data using dao method
   service.getuser(username);
   }catch(ServiceException ex){
  throw new BusinessException(ex.getMessage());
  }
  
   return user;
  }
  }
  </code></pre>
  
  <p>In presentation layer , let it be a controller class</p>
  
  <pre><code> class Presentation{
   public User getUser(String username) throws BusinessException{
  
    User user = null;
  
  
   //some operation to fetch data using business method
   business.getUser(username);
  
    return user;
   }
   }
  </code></pre>
  
  <p>Assume from the presentation layer message is thrown to the user in the front end JSP page ..</p>