我应该为数据库查找扔一个KeyNotFoundException? [英] Should I throw a KeyNotFoundException for a database lookup?

查看:156
本文介绍了我应该为数据库查找扔一个KeyNotFoundException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,给定一个ID,从数据库返回一个项目。如果没有任何项目与给定的ID匹配,是否适合抛出
解决方案

根据您用于访问数据库(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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆