Uglypattern of the day:
Uglypattern of the day:
MyObject := nil;
try
MyObject := TSomething.Create();
...
finally
if assigned(MyObject) then
MyObject.Free;
end;
It's so close to being right. So close.
MyObject := nil;
try
MyObject := TSomething.Create();
...
finally
if assigned(MyObject) then
MyObject.Free;
end;
It's so close to being right. So close.
Hm, the rule that a constructor should never raise an exception is new to me. It makes sense, now that I think about it and makes lots of decisions much easier (e.g. should I open that file in the constructor or have an additional method for doing that?). But it can't be widely known otherwise there wouldn't be so many libraries that violate that rule.
ReplyDeleteBut what about something like this:
Type
TDirObj = class
constructor Create(const _Path: string);
procedure EnumSubdirs(_List: TStrings);
end;
Should the constructor raise an exception if the directory does not exist? If not, what should happen, if EnumSubdirs gets called for such an object?
Daniela Osterhagen I would check for the directory's existence in EnumSubDirs. There's no guarantee that the directory will even still exist by the time EnumSubDirs is called.
ReplyDeleteRight, it might no longer exist. That's true.
ReplyDelete