在构造函数中抛出 ArgumentNullException?

发布于 2024-09-17 23:12:46 字数 95 浏览 4 评论 0原文

对于具有单个参数的构造函数,如果参数为 null/空,可以在构造函数内部抛出 ArgumentNullException 吗?或者,应该将其抛出到实际使用参数的方法中吗?谢谢。

For a constructor with a single parameter, is it OK to throw an ArgumentNullException inside the constructor if the parameter is null/empty? OR, should it be thrown in the method that actually uses the argument? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风启觞 2024-09-24 23:12:46

是的,如果这是完全必要的,那么抛出异常。您不应该*稍后抛出异常。

永远记住“尽早失败原则”。概念现在失败了,因此您不会浪费时间调试或遇到意外的系统功能。

或者,您也可以针对“”抛出 ArgumentException,针对 null 抛出 ArgumentNullException。无论哪种情况,请确保抛出有效的异常消息。


始终是管理异常的好参考文章:良好的异常管理拇指规则


Side请注意 @Steve Michelotti 所说的内容(因为我是 CodeContracts 的忠实粉丝)

Contract.Requires<ArgumentNullException>(inputParemeter!= null, "inputparameter cannot be null");
Contract.Requires<ArgumentException>(inputParemeter!= "", "inputparameter cannot be empty string");

Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(inputParemeter), "inputparameter cannot be null or empty string");

Yes, if it is completely essential then throw the exception. You should not* throw the exception later.

Always remember the "Fail Early Principle". Concept being fail now, so you don't waste time debugging or experience unexpected system functionality.

Alternatively you could also throw a ArgumentException for "" and ArgumentNullException for null. In either case make sure you throw a valid Exception message.


Always a good reference article for managing exceptions: Good Exception Management Rules of Thumb


Side note on what @Steve Michelotti said (because i am a huge fan of CodeContracts)

Contract.Requires<ArgumentNullException>(inputParemeter!= null, "inputparameter cannot be null");
Contract.Requires<ArgumentException>(inputParemeter!= "", "inputparameter cannot be empty string");

alternatively

Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(inputParemeter), "inputparameter cannot be null or empty string");
计㈡愣 2024-09-24 23:12:46

将其放入构造函数中就可以了 - .NET 框架中有几个类可以执行此操作。此外,请查看代码合同

Throwing it in the constructor is fine - there are several classes in the .NET framework that do this. Additionally, check out code contracts for this.

兔小萌 2024-09-24 23:12:46

听起来,您将一个参数传递到构造函数中,该参数由该类保存,以便稍后在其他方法中使用。如果您实际上没有在构造函数中使用该参数,您可能应该考虑将该参数移动为实际使用它的方法的参数。

From what it sounds like, you pass in a parameter into the constructor to be held by the class for use in some other method later on. If you're not actually using the argument in the constructor, you should probably think about moving the argument to be a parameter of the method that's actually using it.

黎歌 2024-09-24 23:12:46

我会将检查放在调用构造函数时设置的属性中......这样在所有情况下都会引发异常。

I'd put the check in the property that you set when the constructor is called... That way the exception would be thrown in all cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文