Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Catch EmptyResultDataAccessException?

I have a SQL statement that I expect will return one row, because I am passing the primary key. So my choices are to

  1. Wrap the queryForObject in a try/catch, catching EmptyResultDataAccessException, and returning null
  2. Change the call to queryForList, and unwrap the List and (hopefully) return the 1st element, or null.

I read somewhere the cathing an EmptyResultDataAccessException, since it extends runtime exception, is a bad practice.

But I can't see anything wrong with it.

I would be interested in hearing opinions

like image 261
EdgeCase Avatar asked May 06 '13 20:05

EdgeCase


1 Answers

I think it's perfectly acceptable to catch that exception. It's the only Spring exception I catch that I can recall, but I have valid use cases for trying to find a record that is potentially not in existence.

like image 123
GriffeyDog Avatar answered Oct 02 '22 12:10

GriffeyDog