Should I throw a KeyNotFoundException for a database lookup? [closed]

依然范特西╮ 提交于 2019-12-13 06:25:27

问题


I've got some code that, given an ID, returns an item from the database. If no item matches the given ID, is it appropriate to throw a KeyNotFoundException, or are such exceptions meant only for Dictionary types?


回答1:


Depending on the framework you're using to access the database (ODBC, ADO.NET, etc.) there's likely a better alternative that the framework would throw.

The System.Data namespace contains several such exceptions as examples: http://msdn.microsoft.com/library/system.data.

Really, though, as long as you document what exception you're throwing and in what conditions it doesn't matter too much. Using something common is best because people may assume, but if they are reading documentation they will handle whatever exception you say would be thrown.




回答2:


The MSDN page for KeyNotFoundException states:

The exception that is thrown when the key specified for accessing an element in a collection does not match any key in the collection.

Semantically a KeyNotFoundException would be appropriate. Just make sure your error message is descriptive enough to actually help when debugging. Also, remember that you lose any contextual type information that using a custom exception type would give you.

A custom exception type gives you the ability to differentiate between the two conditions using general C# idioms, like so:

try 
{
  // do some database stuff
} 
catch(KeyNotFoundException e) 
{
  // Here you know that it was more than likely caused by a Dictionary 
  // or some such collection
}
catch(DatabaseKeyNotFoundException e)
{
  // Here you know that it was caused by a DB record not being found
}

Whichever you choose, just make sure you think about it, that it's consistent and logical. Don't just choose one because it's easier.




回答3:


MSDN says yes* (if you consider a database table a collection) -

The exception that is thrown when the key specified for accessing an element in a collection does not match any key in the collection.

Why not just return false or some other indication that the value was not in the database?



来源:https://stackoverflow.com/questions/21146594/should-i-throw-a-keynotfoundexception-for-a-database-lookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!