<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p><br>
</p>
<div class="moz-cite-prefix">On 1/24/25 4:04 AM, Hairy Pixels via
fpc-pascal wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAGsUGt=ZJCnxbQm_HdoVB1XfQX+T_j9KKGP_k0-i_U_hXduYbQ@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Jan 24, 2025 at 8:38:25 AM,
Nikolay Nikolov via fpc-pascal <<a
href="mailto:fpc-pascal@lists.freepascal.org"
moz-do-not-send="true" class="moz-txt-link-freetext">fpc-pascal@lists.freepascal.org</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote"
style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"
type="cite"> Maybe because there's a much better way to write
it:<br>
<br>
procedure foo();<br>
var<br>
s1: TStringList = nil;<br>
s2: TStringList = nil;<br>
s3: TStringList = nil;<br>
begin<br>
try<br>
s1:=TStringList.create;<br>
s2:=TStringList.create;<br>
s3:=TStringList.create;<br>
doWhatever(s1,s2,s3);<br>
finally<br>
FreeAndNil(s3);<br>
FreeAndNil(s2);<br>
FreeAndNil(s1);<br>
end;<br>
end;<br>
<br>
Best regards,<br>
<br>
Nikolay </blockquote>
</div>
<br>
<div dir="ltr">I’ve basically never used try..finally in my own
code but I have seen really ugly nested like about in other
projects.</div>
</blockquote>
Then your code will probably cause memory leaks in case of
exceptions, and even worse - leaks of other resources (file handles,
sockets, etc.).<br>
<blockquote type="cite"
cite="mid:CAGsUGt=ZJCnxbQm_HdoVB1XfQX+T_j9KKGP_k0-i_U_hXduYbQ@mail.gmail.com">
<div dir="ltr"> This a down stream consequence of exceptions
right?</div>
</blockquote>
<p>That's a downstream consequence having error handling with proper
cleanup, regardless of whether the language uses exceptions, or
not. Consider the case, without exceptions, as is typical for
procedural APIs:</p>
<p>function foo: LongInt;</p>
<p>var</p>
<p> s1, s2, s3: TStringList;<br>
</p>
<p>begin</p>
<p> s1 := Create_StringList;</p>
<p> if s1=nil then</p>
<p> begin</p>
<p> Result := error;</p>
<p> exit;<br>
</p>
<p> end;</p>
<p> s2 := Create_StringList;</p>
<p> if s2=nil then</p>
<p> begin</p>
<p> Free_StringList(s1);</p>
<p> Result := error;</p>
<p> exit;<br>
</p>
<p> end;</p>
<p> s3 := Create_StringList;</p>
<p> if s3=nil then</p>
<p> begin</p>
<p> Free_StringList(s1);</p>
<p> Free_StringList(s2);</p>
<p> Result := error;</p>
<p> exit;<br>
</p>
<p> end;<br>
</p>
<p> if not doWhatever(s1, s2, s3) then</p>
<p> begin</p>
<p> Free_StringList(s1);</p>
<p> Free_StringList(s2);</p>
<p> Free_StringList(s3);</p>
<p> Result := error;</p>
<p> exit;<br>
</p>
<p> end;</p>
<p> {...etc.}</p>
<p> Free_StringList(s1);</p>
<p> Free_StringList(s2);</p>
<p> Free_StringList(s3);<br>
</p>
<p>end;</p>
<p>As you can see, it's more complicated and more error prone. You
can simplify it a little bit with goto, but it will never be
better, compared to try...finally</p>
<p>Best regards,</p>
<p>Nikolay</p>
</body>
</html>