简体   繁体   English

我应该为意外的空值抛出什么异常?

[英]What exception should I throw for an unexpected null value?

I've been a .NET developer for over a decade so here's a shameful question I've never known the answer to. 十多年来我一直是.NET开发人员,所以这是一个我从未知道答案的可耻问题。 I get it--if an argument is null, I can throw an ArgumentNullException. 我明白了 - 如果参数为null,我可以抛出ArgumentNullException。 A NullReferenceException will be thrown if I try to dereference a null value. 如果我尝试取消引用空值,将抛出NullReferenceException。

But what if I have code like the following: 但是如果我有如下代码怎么办:

var someVitalObject = someServiceReference.GetVitalObject();

if (someVitalObject == null)
{
  throw new IDontKnowWhatException(); // what exception should I throw here?
}

Now, this isn't necessarily a problem with the service for which an exception should have been thrown earlier. 现在,这不一定是早先应该抛出异常的服务的问题。

It's hard to say without seeing more context, but perhaps System.InvalidOperationException ? 没有看到更多的上下文很难说,但也许System.InvalidOperationException

The exception that is thrown when a method call is invalid for the object's current state. 方法调用对于对象的当前状态无效时引发的异常。

I would only use System.ArgumentNullException when directly checking a method parameter, not when validating the result of some call. 直接检查方法参数时,我只会使用System.ArgumentNullException,而不是在验证某些调用的结果时。

The type of exception I throw depends greatly on the context. 我抛出的异常类型在很大程度上取决于上下文。 But in this case I would probably go for a custom exception like: 但在这种情况下,我可能会去寻找一个自定义异常:

public class VitalObjectNotAcquiredException : Exception { ... }

I typically use ArgumentNullException for objects passed into a function. 我通常对传递给函数的对象使用ArgumentNullException Anything else null related I use InvalidOperationException . 任何其他null相关我使用InvalidOperationException In specialized cases I'll create a custom exception if it makes sense to. 在特殊情况下,如果有意义,我将创建一个自定义异常。

I would throw a System.NullReferenceException . 我会抛出一个System.NullReferenceException Or, if you're unhappy with that, you could derive from that with a more specialized type of null reference exception for your own purposes. 或者,如果您对此不满意,可以使用更专业的null引用异常来为您自己的目的派生。

Consider to create your own Exception class inherited from the ApplicationException or from the Exception : 考虑创建自己的Exception类,该类继承自ApplicationExceptionException

public sealed class MyException : Exception
{
    ...
}

Thus you will be able to control what kind of information to store in it. 因此,您将能够控制要存储在其中的信息类型。

Personnally, I'd choose according to the GetVitalObject method contract. 个人而言,我会根据GetVitalObject方法合同选择。 If this method should return a not null object I'd change it so it throws an exception in this case. 如果此方法应该返回一个非null对象,我会更改它,因此在这种情况下它会引发异常。

If you don't have control over the method or if returning a null value is correct (but not in your context) i'd go with a custom exception as Mark and Dmitry already said. 如果您无法控制该方法或者返回null值是正确的(但不是在您的上下文中),我会选择自定义异常,因为Mark和Dmitry已经说过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当工厂方法返回null时,应该抛出什么异常? - What exception should I throw when a factory method returns null? 如果缺少配置值,我应该抛出什么异常? - What exception should I throw if a configuration value is missing? 我应该抛出什么异常并应该提供消息? - What exception should I throw and should I provide a message? 将未知值传递给switch语句时,我应该抛出什么类型的Exception - What type of Exception should I throw when an unknown value is passed into a switch statement 当方法返回无效结果时,我应该抛出什么异常 - What exception should I throw when method returns invalid result 对于失败的文件解析,我应该抛出什么异常? - What exception should I throw for a failed file parsing? 如果我在Observable的Subscribe回调中引发异常,应该怎么办? - What should happen if I throw an exception in my Subscribe callback for an Observable? 当扩展方法中的this参数为null时,我应该抛出什么? - What should I throw when the `this` parameter is null in extension methods? 是否应引用null变量引发异常 - Should a reference to a null variable throw exception 我如何找到导致null异常的原因? 我的意思是异常抛出哪行或在代码中的何处? - How can i find what cause a null exception ? I mean what line or where in the code the exception throw?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM