Quantcast
Channel: What exception to throw when a property setter is not allowed? - Stack Overflow
Viewing all articles
Browse latest Browse all 6

What exception to throw when a property setter is not allowed?

$
0
0

I have a base class that has a virtual property:

public virtual string Name { get; set; }

then I have a derived class that overrides only the getter of the property

public override string Name{    get { return "Fixed Name"; }}

The problem is this only overrides the getter. If someone calls the setter the base class setter will get called, and the caller won't know that it's ineffective.

So I thought I'd do something like:

public override string Name{    get { return "Fixed Name"; }    set { throw new Exception(); } //which exception type?}

So two (related questions):

  1. Is there a better pattern for me to use?
  2. If I should be using the above pattern, what exception to use?

Edit: Some reasons why one exception would be preferred over another would be good. My co-worker and I have had the same argument between NotSupported and InvalidOperation.


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles



Latest Images