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

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

问题描述

从属性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.

推荐答案

微软有关于如何在 http://msdn.microsoft.com/en-us/library上设计属性/ms229006.aspx

本质上,他们建议属性getter是轻松的访问器,它们始终可以安全地调用。如果异常是你需要抛出的东西,他们建议重新设计getter作为方法。对于设置器,他们指出异常是一个适当且可接受的错误处理策略。

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.

对于索引器,Microsoft表示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.

有一些很好的理由,为什么你不想抛出异常在财产中:

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


  • 由于属性出现是字段,并不总是很明显,他们可以抛出(按设计)例外;而使用方法,程序员受到训练,期望和调查异常是否是调用方法的预期后果。

  • Getters被许多.NET基础架构所使用,如序列化程序和数据绑定(in例如WinForms和WPF) - 处理这种上下文中的异常可能会迅速成为问题。

  • 当您观察或检查对象时,属性getter会自动由调试器评估。这里的一个例外可能会让您感到困惑,并减慢您的调试工作。出于同样的原因,在属性(如访问数据库)中执行其他昂贵的操作也是不希望的。

  • 属性常常用于链接约定: obj.PropA .AnotherProp.YetAnother - 使用这种语法,决定在何处注入异常catch语句成为问题。

  • 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天全站免登陆