Rya*_*ers 51 c# null constructor arguments exception
对于具有单个参数的构造函数,如果参数为null/empty,是否可以在构造函数内抛出ArgumentNullException?或者,是否应该在实际使用参数的方法中抛出?谢谢.
Nix*_*Nix 54
是的,如果它是完全必要的,那么抛出异常.你不应该在以后抛出异常.
永远记住"早期失败原则".概念现在失败,因此您不必浪费时间调试或体验意外的系统功能.
或者,您也可以为""抛出ArgumentException,为null抛出ArgumentNullException.在任何一种情况下,请确保抛出有效的Exception消息.
始终是管理异常的好参考文章:良好的异常管理规则
关于@Steve Michelotti所说的内容的旁注(因为我是CodeContracts的忠实粉丝)
Contract.Requires<ArgumentNullException>(inputParemeter!= null, "inputparameter cannot be null");
Contract.Requires<ArgumentException>(inputParemeter!= "", "inputparameter cannot be empty string");
Run Code Online (Sandbox Code Playgroud)
或者
Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(inputParemeter), "inputparameter cannot be null or empty string");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17265 次 |
| 最近记录: |