consider following code:
int myid = 10; try { var me = this.db.people.single(p => p.id = myid); } catch(invalidoperationexception e) { // either don't exist ("sequence contains no elements"), // or more 1 of me exists ("sequence contains more 1 element") }
there 2 conditions: either 0 results returned (which might not alarming, person doesn't exist), or more 1 result returned (which means wrong assuming id supposed unique).
ideally, catch , handle exception if thrown because 0 results returned, not catch exception if thrown because more 1 results returned (i can't handle situation, don't want ignore went wrong).
is there way differentiate between 2 exceptions can handle case in 0 results returned, or option use singleordefault , check null?
the answer here:
either 0 results returned (which might not alarming, person doesn't exist),
this isn't "exceptional" because said, person might not exist. shouldn't throw exception when happens. it's going website , searching product. if no results returned that's not error. means don't sell product.
or more 1 result returned (which means wrong assuming id supposed unique).
if should impossible more 1 result, more 1 result should throw exception.
that makes singleordefault
best choice. check null, because query might not return anything. if out of bounds happens there should exception.
Comments
Post a Comment