- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
对于具有单个参数的构造函数,如果参数为 null/空,是否可以在构造函数内部抛出 ArgumentNullException?或者,它应该在实际使用参数的方法中抛出吗?谢谢。
最佳答案
是的,如果它是完全必要的,那么抛出异常。你以后不应该*抛出异常。
永远记住"Fail Early Principle" .概念现在失败了,所以您不会浪费时间调试或遇到意外的系统功能。
或者,您也可以为“”抛出 ArgumentException,为 null 抛出 ArgumentNullException。无论哪种情况,请确保抛出有效的异常消息。
始终是管理异常的好引用文章:Good Exception Management Rules of Thumb
@Steve Michelotti 所说的旁注(因为我是 CodeContracts 的 super 粉丝)
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");
关于c# - 在构造函数中抛出 ArgumentNullException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3629849/
这个问题在这里已经有了答案: Use of ArgumentNullException when accessing arguments' properties (1 个回答) 关闭 2 年前。 我
我有一个在管理控制台和服务器应用程序之间使用远程处理的 .NET 应用程序。 在运行时间特别长的远程调用期间,我会收到一条错误消息,如下所示。 我已将 Sponsor 添加到客户端以防止 Remoti
我有一个关于构造函数的语法和在构造函数中抛出异常的问题。 如何在调用 CreateAnotherOne() 之前为参数 b 抛出 ArgumentNullException 并像在第二个构造函数中那样
我试图将我的位图保存到 MemoryStream - 这段代码有什么问题?为什么它让我 argumentnullexception ? private void insertBarCodesToPDF
我在整个过程中都使用了这个代码模式,并且只在这个特定的属性中尝试为其赋值(即调用 setter)时抛出 ArgumentNullException。为null的对象是isLoggedInLock,因此
当有 null 值时抛出 ArgumentNullException() 是个好主意吗? This thread 没有提到在 null 上抛出的最明显的异常。 谢谢 最佳答案 ArgumentNull
假设我有一个类(class) Foo具有复杂的属性 Bar .然后,假设我在其他类中有一个如下所示的方法: public void DoSomething(Foo foo) { if (foo
我正在尝试编写 XUnit 测试来测试我的自定义验证器。验证器检查其他属性的值,该值指示已验证的属性是否应为 null 或具有值。但是,当我使用 TryValidateProperty 方法时,测试返
你好, 我有一个使用 sql express 本地数据库的程序。我希望能够使用该程序运行必要的脚本来更新该数据库。一个文本文件已作为嵌入式资源添加到项目(VS2010)中,该文件包含文本。但是它无法打
我正在开发 MVC 4 C# 互联网应用程序。 我有一个 MapLocationCompany 类和一个 MapLocation 类。我在每个 MapLocationCompany 对象之后都有一个
我注意到这段代码经常出现在我的构造函数中: if (someParam == null) throw new ArgumentNullException("someParam"); if (someO
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
所以我正在尝试更改 Windows 8 应用程序中的框架。我尝试按照 this page 上的教程进行操作,但我不断收到同样的错误。 我在线上收到 ArgumentNullException: fra
对于具有单个参数的构造函数,如果参数为 null/空,是否可以在构造函数内部抛出 ArgumentNullException?或者,它应该在实际使用参数的方法中抛出吗?谢谢。 最佳答案 是的,如果它是
假设我有一个方法将某种对象作为参数。现在说如果这个方法传递了一个空参数,这是一个 fatal error ,应该抛出一个异常。编写这样的代码对我来说是否值得(记住这是一个微不足道的例子): void
我正在尝试使用 Windows Azure PowerShell 模块来管理订阅。 我已经下载了我的证书(.publishsettings 文件)并使用 Import-AzurePublishSett
我有一个简单的方法,该方法从Web服务获取人员信息,并使用StringBuilder构建格式化的字符串。 public List Process(data) { List returnVal =
嗨,我有一个名为 Activity 的类, 在一个表单上,我将其对象数组创建为, Activity[] _actList; 然后这样做, List termsList = _actList.ToLi
我正在阅读 this post回答者提到他更喜欢 ArgumentNullException 而不是 NullReferenceException。 MSDN提到 NullReferenceExcep
当我尝试在 Xamarin Forms 中使用 XAML 创建内容 View 时,生成的代码隐藏会抛出此错误: System.ArgumentNullException: Value cannot b
我是一名优秀的程序员,十分优秀!