<!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 8:38 AM, Hairy Pixels via
      fpc-pascal wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAGsUGt=gdVYi2cpBdgtSdUVOwZ116m6EOifBsDXoCgCM8RYFRw@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 1:34:26 PM,
          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"> Doesn't matter whether they're handled in the
          same scope or not. It's the same code. Usually they're not
          handled in the same scope, but in a very distant place. That's
          usually when exceptions are convenient.<br>
          Once you let them bubble up you need to wrap all call sites
          with try..finally right?<br>
          Wrong. Only sites that allocate memory, that needs to be
          freed, before leaving the scope need a try..finally. You don't
          need to wrap every call. That's nonsense. The pattern is:<br>
          <br>
          var<br>
          <br>
            {... initialize local vars you're going to allocate in this
          function with nil}<br>
          <br>
          begin<br>
          <br>
            try<br>
          <br>
              {lots of code, allocations, calls, etc}<br>
          <br>
            finally<br>
          <br>
              {cleanup code}<br>
          <br>
              {free stuff that isn't nil}<br>
          <br>
            end;<br>
          <br>
          end;<br>
          <br>
        </blockquote>
      </div>
      <div><br>
      </div>
      Yes that’s what I meant. If you don't know which calls could fail
      you need to wrap all functions that allocate memory in
      try..finally. That’s what the original post was commenting on, the
      need for some other syntax which knows to free a variable if the
      function terminates early due to an exception, like the implicit
      exception frames for managed types like dynamic arrays.<br>
    </blockquote>
    <p>Yes, that could probably save some typing, however it comes at a
      price:</p>
    <p>1) only works for classes</p>
    <p>2) not obvious order of destructor calls. What if they have side
      effects, and the order matters?</p>
    <p>3) doesn't support procedural APIs.</p>
    <p>In really, I find try..finally to be much nicer, because you can
      do a proper cleanup in the finally section that includes any of
      the following:</p>
    <p>* destructor calls</p>
    <p>* FreeMem</p>
    <p>* Dispose</p>
    <p>* function calls (e.g. Close(), XFree(), etc.)<br>
    </p>
    <p>And the order is well defined by you.</p>
    <p>So, I prefer try..finally. And it's not much typing, when used
      properly.</p>
    <p>Nikolay<br>
    </p>
  </body>
</html>