最佳实践:从属性中抛出异常 [英] Best practices: throwing exceptions from properties

查看:18
本文介绍了最佳实践:从属性中抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候从属性 getter 或 setter 中抛出异常合适?什么时候不合适?为什么?指向有关该主题的外部文档的链接会有所帮助……谷歌的搜索量出乎意料地少.

When is it appropriate to throw an exception from within a property getter or setter? When is it not appropriate? Why? Links to external documents on the subject would be helpful... Google turned up surprisingly little.

推荐答案

Microsoft 在 http://msdn.microsoft.com/en-us/library/ms229006.aspx

Microsoft has its recommendations on how to design properties at http://msdn.microsoft.com/en-us/library/ms229006.aspx

本质上,他们建议属性 getter 是轻量级的访问器,它们总是可以安全调用.如果您需要抛出异常,他们建议将 getter 重新设计为方法.对于 setter,它们表明异常是一种适当且可接受的错误处理策略.

Essentially, they recommend that property getters be lightweight accessors that are always safe to call. They recommend redesigning getters to be methods if exceptions are something you need to throw. For setters they indicate that exceptions are an appropriate and acceptable error handling strategy.

对于索引器,微软表示可以接受 getter 和 setter 都抛出异常.事实上,.NET 库中的许多索引器都是这样做的.最常见的异常是 ArgumentOutOfRangeException.

For indexers, Microsoft indicates that it is acceptable for both getters and setters to throw exceptions. And in fact, many indexers in the .NET library do this. The most common exception being ArgumentOutOfRangeException.

您不想在属性 getter 中抛出异常有一些很好的理由:

There are some pretty good reasons why you don't want to throw exceptions in property getters:

  • 因为属性看起来"是字段,所以它们可以抛出(按设计)异常并不总是很明显;而对于方法,程序员被训练去预期和调查异常是否是调用方法的预期结果.
  • 许多 .NET 基础架构都使用了 Getter,例如序列化程序和数据绑定(例如在 WinForms 和 WPF 中)——处理此类上下文中的异常很快就会出现问题.
  • 当您观察或检查对象时,调试器会自动评估属性 getter.这里的例外可能会令人困惑并减慢您的调试工作.出于同样的原因,在属性中执行其他昂贵的操作(例如访问数据库)也是不可取的.
  • 属性通常用于链接约定:obj.PropA.AnotherProp.YetAnother - 使用这种语法,决定在何处注入异常捕获语句变得有问题.
  • Because properties "appear" to be fields, it is not always apparent that they can throw a (by-design) exception; whereas with methods, programmers are trained to expect and investigate whether exceptions are an expected consequence of invoking the method.
  • Getters are used by a lot of .NET infrastructure, like serializers and databinding (in WinForms and WPF for example) - dealing with exceptions in such contexts can rapidly become problematic.
  • Property getters are automatically evaluated by debuggers when you watch or inspect an object. An exception here can be confusing and slow down your debugging efforts. It's also undesirable to perform other expensive operations in properties (like accessing a database) for the same reasons.
  • Properties are often used in a chaining convention: obj.PropA.AnotherProp.YetAnother - with this kind of syntax it becomes problematic to decide where to inject exception catch statements.

作为旁注,人们应该意识到,仅仅因为一个属性没有设计来抛出异常,并不意味着它不会;它可以很容易地调用这样做的代码.即使是分配新对象(如字符串)的简单操作也可能导致异常.您应该始终防御性地编写代码,并期望从您调用的任何内容中获得异常.

As a side note, one should be aware that just because a property is not designed to throw an exception, that doesn't mean it won't; it could easily be calling code that does. Even the simple act of allocating a new object (like a string) could result in exceptions. You should always write your code defensively and expect exceptions from anything you invoke.

这篇关于最佳实践:从属性中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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