Should I raise an exception when "caseing" enum type is out of bounds, like:

Should I raise an exception when "caseing" enum type is out of bounds, like:

procedure TSomeClass.SetSomeProperty(const Value: TSomeEnumType);
begin
 FValue := Value;
  case Value of
    enumOne:;      
    enumTwo:;    
    enumThree:;   
    enumFour:;
    else
      raise Exception.Create('Unknow enum');
  end;
end;

or it's unnecessary?

Comments

  1. Didn't there use to be a hint or a warning for case statements that doesn't cover all possibilities?

    This compiles without an hint or warning in XE3.
    Is there a compiler setting which govern this?

    type
      TMyEnum = (a, b, c, d);

    function MyEnumToStr(const aEnum:TMyEnum):String;
    begin
      case aEnum of
        a: Result := 'a';
        b: Result := 'b';
        c: Result := 'c';
      end;
    end;

    ReplyDelete
  2. Lars Fosdal I'm pretty sure that in Delphi 7 you could get "function may not return a value" etc if you did that.

    ReplyDelete
  3. Moz Le - You are right - there should be two messages here.
    One for the potentially undefined result, and one for potentially uncovered cases.

    ReplyDelete

Post a Comment