当方法尝试使用可以为空的字段时抛出哪个异常?
我实际上正在从事框架开发,这意味着需要非常强大的编码方法。
我面临一个问题,我不知道需要抛出哪个 System.Exception 派生类。基本上情况是,我有一个类,其中的字段可以由构造函数选择性地初始化,并且具有使用这些字段的方法。如果用户没有初始化这些字段,我必须抛出哪个异常? (这意味着它们为空)
这是一个示例:
public class MyConnection
{
private Uri endpointUri;
public Uri EndpointUri
{
get
{
return this.endpointUri;
}
set
{
this.endpointUri = value;
}
}
public MyConnection()
{
}
public MyConnection(Uri endpointUri)
{
this.endpointUri = endpointUri;
}
public FileStream GetFile()
{
if (this.endpointUri != null)
{
// My doer methods
}
else
{
throw new TheExceptionINeedToThrow("endpointUri", ...);
}
}
}
请注意,我一直在阅读整个 “框架设计指南”一章涉及异常处理和抛出,我没有找到任何适合这种情况的解决方案。或者也许我误解了一些东西......
感谢您的帮助。
编辑:事实上,我提供一个空的构造函数对于我的问题来说似乎有点令人困惑,但它完全是自愿的。在某些必须遵守一系列不同状态且无法在多个对象中复制的对象中,它有时很有用。
I am actually working on a Framework development, which means require a really strong coding methodology.
I am facing a problem where I do not know which System.Exception derivated class I need to throw. Basically the case is when I have a class with fields that can be optionnaly initialized by the constructor and that have methods using these fields. Which exception must I throw if the user did not initialized these fields? (which means they are null)
Here is an example:
public class MyConnection
{
private Uri endpointUri;
public Uri EndpointUri
{
get
{
return this.endpointUri;
}
set
{
this.endpointUri = value;
}
}
public MyConnection()
{
}
public MyConnection(Uri endpointUri)
{
this.endpointUri = endpointUri;
}
public FileStream GetFile()
{
if (this.endpointUri != null)
{
// My doer methods
}
else
{
throw new TheExceptionINeedToThrow("endpointUri", ...);
}
}
}
Note that I have been reading the whole "Framework Design Guidelines" chapter concerning exception handling and throwing and that I did not found any solution fitting this exact case. Or maybe I misunderstood something ...
Thanks for your help.
EDIT : The fact that I provide an empty constructor seems a bit confusing regarding my problem but it is completely voluntary. In some objects that have to comply with a range of different states that cannot be duplicated in multiple objects it is sometimes useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抛出
InvalidOperationException
:请注意,空引用没有被传递到方法中 - 当调用方法时它已经存在 - 因此无效的是对象的当前状态,而不是参数。
但是,如果可能的话,最好从一开始就阻止以这种方式创建对象 - 它必须是可写属性吗?您是否想要一个具有空端点 URI 的实例?
Throw
InvalidOperationException
:Note that the null reference isn't being passed into the method - it's already there when the method is called - so it's the object's current state which is invalid, not an argument.
However, it would be better to prevent the object from being created in this way to start with, if at all possible - does it have to be a writable property? Would you ever want an instance which did have a null endpoint URI?
只要异常描述清楚地说明什么是 null,
NullReferenceException
、InvalidArgumentExecption
或ApplicationException
都可以。NullReferenceException
,InvalidArgumentExecption
orApplicationException
would all be fine as long as the exception description clearly states what it is that is null.与其他人一样,我会推荐
InvalidOperationException
(因为 John Skeet 说过):)如果您调用带有 null 参数的函数,那么我将使用
ArgumentNullException
。Like the others I will recommend
InvalidOperationException
(Because John Skeet said it) :)If you call a function with a null parameter then i will use
ArgumentNullException
.