异常当期待空值时抛出? [英] Exception to throw when expecting a null value?

查看:145
本文介绍了异常当期待空值时抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我期待一个空值并获得一个定义的值(在一个属性的getter内),并且想要抛出一个异常,那么在csharp中执行此操作的正确方法是什么?在这种情况下,是否有任何定义是有意义的?

If I am expecting a null value and get a defined value (within a getter of a property) and want to throw an exception, what would be the proper way to do this in csharp? Is there anything defined already that makes sense in this situation?

推荐答案

我的猜测是:

throw new
    ArgumentException("Parameter was expected to be null, value was provided.");

ArgumentOutOfRangeException 也可以正常工作,但通常当有一个明确的范围而不是null而不是null时使用。

ArgumentOutOfRangeException might also work, but is typically used when there is a well defined range rather than null vs. not null.

这篇关于异常当期待空值时抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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