@property - take it behind the woodshed and shoot it?

Jonathan M Davis jmdavisProg at gmx.com
Fri Jan 25 11:30:31 PST 2013


On Friday, January 25, 2013 17:37:48 eles wrote:
> On Thursday, 24 January 2013 at 08:35:01 UTC, Walter Bright wrote:
> 
> One more thing (see also this:
> http://www.25hoursaday.com/CsharpVsJava.html#properties)
> 
> In order to avoid properties throwing exceptions, maybe is wise
> to impose getters and setters to be nothrow.
> 
> Why? Because code like this (C#) seems a bit unnatural:
> 
> try{
> 
> myClock.Hours = 28; /* setter throws exception because 28 is
> an invalid hour value */
> myClock.Minutes = 15;
> myClock.Seconds = 39;
> 
> }catch(InvalidTimeValueException itve){
> 
> /* figure out which field was invalid and report error */
> 
> }

And why wouldn't setters be able to throw. Why on earth would you do other 
than throw in code like you have above? How on earth would a setter report a 
bad argument if it was forced to be nothrow? Exceptions are the correct 
solution. Setters _need_ to be able to throw. Plenty of them don't need to, 
but plenty of them do. For instance, many of the properties in std.datetime 
will throw if you give them invalid values (like setting the hour to 28). I 
don't see how you could possibly argue that a setter should not be able to 
throw.

- Jonathan M Davis


More information about the Digitalmars-d mailing list