[英]When to raise an exception or return null?
I have a few functions on Data access layer 我在数据访问层上有一些功能
public Order RetrieveById(int id)
public List<Order> RetrieveByStatus(OrderStatus status)
Now i am bit confuse on exception raising. 现在我对异常提升有点困惑。
In case of RetrieveById function, id which is less than 1 is an invalid id therefore i feel like raising an exception. 在RetrieveById函数的情况下,小于1的id是无效的id,因此我想提出异常。 And i feel like returning null for the Id which doesn't exist in the database.
我想为数据库中不存在的Id返回null。 Then it feels like i am over complicating.
然后感觉就像我过度复杂。
In case of RetrieveByStatus, i feel like returning a empty list when there is no data in the database for that status. 对于RetrieveByStatus,我觉得当数据库中没有该状态的数据时返回一个空列表。
However i have seen that some people raising an exception when RetrieveById cannot return anything but then RetrieveByStatus should not raise exception when there is no record or should it? 但是我看到有些人在RetrieveById无法返回任何内容时会引发异常,但是当没有记录时,RetrieveByStatus不应该引发异常或应该是什么?
Could anyone please clarify these concepts for me? 谁能请我为这些概念澄清一下?
In the first case i would possibly go for a exception and handle myself,instead of returning null.What if your first method is used in a way that the returned object is saved to a Order reference.There is a very high chance of NullReferenceException
being thrown,when someone tries to call a method or property on that object. 在第一种情况下,我可能会去一个例外和处理自己,而不是如果你的第一种方法是在返回的对象保存到一个令reference.There方式使用返回null.What是非常高的机会
NullReferenceException
是抛出,当有人试图调用该对象上的方法或属性时。
For the second method i would go for a empty list as some have suggested. 对于第二种方法,我会按照一些人的建议去寻找空列表。
I would prefer to return
null
in the first case and anempty list
in the second case.我宁愿在第一种情况下返回
null
,在第二种情况下返回empty list
。
But if you want to raise exception then You can raise exception for public Order RetrieveById(int id)
because it means that id
is not valid as calling the first method means that you know the id
and the it needs to be there. 但是如果你想引发异常那么你可以为
public Order RetrieveById(int id)
引发异常,因为这意味着id
无效,因为调用第一个方法意味着你知道id
并且它需要在那里。
In the second case the OrderStatus
might be valid and there is not record found against it so returning an empty list is a good idea. 在第二种情况下,
OrderStatus
可能是有效的,并且没有找到针对它的记录,因此返回空列表是个好主意。
In any case you'll have to process either null return or an exception thrown 在任何情况下,您都必须处理null返回或抛出异常
As for myself, I'd prefer in both your methods not to throw exception explicitly. 至于我自己,我更喜欢你的两种方法都不要明确地抛出异常。 I'd say, there is nothing bad, if your method returns null, if it failed to find an object by id.
我会说,没有什么不好,如果你的方法返回null,如果它无法通过id找到一个对象。 Whereas the
RetrieveByStatus
method could return an empty collection, not null. 而
RetrieveByStatus
方法可以返回空集合,而不是null。
Besides you could follow the pattern used in LINQ, where you have, say, Enumerable.First and Enumerable.FirstOrDefault methods (either throwing an exception or returning null), so you could use the appropriate one in a certain situation, when the id
is 100% valid or when on the contrary it could be missing. 除此之外,您可以遵循LINQ中使用的模式,例如Enumerable.First和Enumerable.FirstOrDefault方法(抛出异常或返回null),因此您可以在特定情况下使用适当的模式,当
id
为100%有效或相反,它可能会丢失。 While methods returning a sequence of elements don't throw exceptions if the sequence to return appears to be empty; 返回序列元素的方法如果返回的序列看起来是空的,则不会抛出异常; consider Enumerable.Where .
考虑Enumerable.Where 。
I like to avoid returning null whenever possible, because NullRefExceptions
are much more cryptic than a specific exception, say OrderNotFoundException
. 我希望尽可能避免返回null,因为
NullRefExceptions
比特定的异常(比如OrderNotFoundException
要神秘得多。 Also, code gets pretty obtuse when you have to constantly expect entities to be null. 此外,当你不得不经常期望实体为空时,代码变得相当迟钝。 This ought to be an exception case anyway -- where did you get that id if it doesn't exist in the db?
无论如何,这应该是一个例外情况 - 如果数据库中不存在该ID,您在哪里获得该ID?
On the cases you suspect this is more likely to throw an error, you could add a DoesObjectExist
or TryGet
type method (or even extension method). 在您怀疑这更可能抛出错误的情况下,您可以添加
DoesObjectExist
或TryGet
类型方法(甚至扩展方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.