A property getter throws an exception other than the few that a getter is expected to throw

tx0/csharp:throw-in-getterrecommendedwarningreliability/error-handlingdepth fastsmell

What it finds

Reading a property looks like reading a field: debuggers evaluate getters to show objects, serializers and data binding read them all, and callers do not expect a read to fail. A getter that throws breaks those uses. InvalidOperationException, ObjectDisposedException, NotSupportedException, NotImplementedException and UnreachableException say that the object is in no state to answer, and are left alone; indexers are left alone too.

From the rule's source, throw_in_getter.rs.

Example from the rule's tests
using System; class Account{    private decimal? balance;     public decimal Balance    {        get        {            if (balance == null)            {                throw new ArgumentException("no balance");tx0 reports: a property getter throws ArgumentException            }            return balance.Value;        }    }     public string Owner => throw new InvalidOperationException("not loaded");     public int this[int i] => throw new ArgumentOutOfRangeException(nameof(i));}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:throw-in-getter" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:throw-in-getter] reason="..."

tx0 explain tx0/csharp:throw-in-getter shows the same in the terminal. Found a false positive? Report it.