Search code examples
.netnotsupportedexceptiondesign-guidelines

Throwing NotSupportedException From Property Getters


I have heard it is inappropriate to throw exceptions from property getters, and I understand the reasons behind this rationale. However, the following situation is puzzling me: Imagine you are writing a facade meant to adapt to several different platforms:

public interface IFacade
{
    int SomeProperty { get; set; }
}

Now imagine that platform X and Y support SomeProperty natively, but that platform Z does not. Shouldn't throwing NotSupportedException from the getter in platform Z's adapter be the right way to tell users that the functionality is not supported in that platform's particular case?


Solution

  • As long as this behavior is documented there is nothing wrong about it. If you are concerned with necessity to handle the exception, you can introduce SupportsSomeProperty property. However, this can blow up the interface.