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?
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?
Didn't there use to be a hint or a warning for case statements that doesn't cover all possibilities?
ReplyDeleteThis 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;
Lars Fosdal I'm pretty sure that in Delphi 7 you could get "function may not return a value" etc if you did that.
ReplyDeleteMoz Le - You are right - there should be two messages here.
ReplyDeleteOne for the potentially undefined result, and one for potentially uncovered cases.