title: Database exception handling best practices
body: <p>How do you handle database exceptions in your application?<br>
  Are you trying to validate data prior passing it to DB or just relying on DB schema validation logic?<br>
  Do you try to recover from some kind of DB errors (e.g. timeouts)?</p>
  
  <p>Here are some approaches:</p>
  
  <ol>
  <li>Validate data prior passing it to DB</li>
  <li>Left validation to DB and handle DB exceptions properly</li>
  <li>Validate on both sides</li>
  <li>Validate some obvious constraints in business logic and left complex validation to DB </li>
  </ol>
  
  <p>What approach do you use? Why?</p>
  
  <p><strong>Updates:</strong>  </p>
  
  <p>I'm glad to see growing discussion.<br>
  Let’s try to sum up community answers.  </p>
  
  <p>Suggestions:  </p>
  
  <ul>
  <li>Validate on both sides</li>
  <li>Check business logic constraints on
  client side, let DB do integrity checks <a href="https://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39406">from hamishmcn</a></li>
  <li>Check early to avoid bothering DB <a href="https://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39461">from ajmastrean</a></li>
  <li>Check early to improve user experience <a href="https://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39428">from Will</a></li>
  <li>Keep DB interacting code in place to
  simplify development <a href="https://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39406">from hamishmcn</a></li>
  <li>Object-relational mapping (NHibernate, Linq, etc.) can help you to deal with constrains <a href="https://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39461">from ajmastrean</a></li>
  <li>Client side validation is necessary for security reasons <a href="https://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39487">from Seb Nilsson</a></li>
  </ul>
  
  <p>Do you have anything else to say? This is converted to Validation specific question. We are missing the core, i.e. "Database related Error best practices" which ones to handle and Which ones to Bubble up?</p>