Question

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?

Was it helpful?

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.

OTHER TIPS

Since you know that exception can't be caught (there's nothing you can do about it, the platform isn't suppported!), or handled if it is caught, it would be better to exit the program and display an error message saying that the current platform is not supported.

Exceptions are generally used in places where they can be caught and handled, or are thrown unexpectedly in event of error. If you catch an error that the program is running on platform z, then exit the program, if it cannot continue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top