当用于返回值的对象为空时,您会从只读属性中抛出什么类型的异常
public class TestClass
{
SomeObject obj;
public string NameOfObject
{
get
{
if(obj == null)
{ // what exception type to throw here }
return obj.Name;
}
}
最佳答案
我会抛出一个 InvalidOperationException
。
ArgumentNullException
我仅在方法参数为 null 时抛出。
如果方法参数处于无效状态,我将抛出一个 ArgumentException
。
关于c# - 抛出什么类型的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4627111/