我应该为数据库查找扔一个KeyNotFoundException? [英] Should I throw a KeyNotFoundException for a database lookup?
问题描述
根据您用于访问数据库(ODBC,ADO.NET等)的框架,框架可能会有更好的选择。
System.Data
命名空间包含几个例外,例如: http://msdn.microsoft.com/library/system.data 。
真的,尽管如此,只要你记录出你抛出什么样的异常,在什么条件下,这并不重要。使用常见的东西是最好的,因为人们可能会认为,但如果他们正在阅读文档,他们将处理你所说的任何异常。
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?
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.
这篇关于我应该为数据库查找扔一个KeyNotFoundException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!