<!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:12 AM, Hairy Pixels via
      fpc-pascal wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAGsUGtnPbF6TrHdNM7ckBQWP63EYej8aFzarh=V1VgiOQ0yhdQ@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
          12:43:51 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"> That's not what exceptions are meant to be used
          for, though. What you describe is called a program "defect".
          When you encounter a "defect" in your program, you terminate
          the program. The proper handling of a defect is to fix
          (modify) the program, so it doesn't happen again. In the case
          of a "defect", there's no much point in freeing the memory,
          because all of the program's memory is freed anyway, when the
          process terminates. An exception is used for reporting an
          unanticipated condition, encountered at runtime. A program can
          be correct (bug free) and still encounter exceptions. For
          example, a web browser might encounter a network error, during
          the loading of a web page, in which case, it should report the
          error to the user in its GUI, and it should continue operating
          normally, instead of crashing, so the user can continue
          browsing, by e.g. trying again, or typing in a different URL,
          or switching to a different tab, etc.<br>
        </blockquote>
        <div class="gmail_quote"><br>
        </div>
        <div class="gmail_quote" dir="ltr">Yes I know I’m just saying
          that’s the most I’ve done with them in Pascal. I do use them
          to escape deep recursion too and then they’re very helpful but
          I would have random allocations in a recursive that could fail
          randomly at any point.</div>
        <br>
        <blockquote class="gmail_quote"
style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"
          type="cite">Feels to me like manual memory management and
          exceptions don’t mix well.<br>
          I fail to understand how you came to this conclusion, since I
          just showed how the same code would look without exceptions,
          and it's way worse.<br>
          <br>
        </blockquote>
        <div class="gmail_quote"><br>
        </div>
        <div dir="ltr">That example was the best scenario for exceptions
          where they’re handled in the same scope they occur and they
          don’t bubble up through the call stack.</div>
      </div>
    </blockquote>
    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>
    <blockquote type="cite"
cite="mid:CAGsUGtnPbF6TrHdNM7ckBQWP63EYej8aFzarh=V1VgiOQ0yhdQ@mail.gmail.com">
      <div class="gmail_quote">
        <div dir="ltr"> Once you let them bubble up you need to wrap all
          call sites with try..finally right?</div>
      </div>
    </blockquote>
    <p>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:</p>
    <p>var</p>
    <p>  {... initialize local vars you're going to allocate in this
      function with nil}<br>
    </p>
    <p>begin</p>
    <p>  try</p>
    <p>    {lots of code, allocations, calls, etc}</p>
    <p>  finally</p>
    <p>    {cleanup code}</p>
    <p>    {free stuff that isn't nil}<br>
    </p>
    <p>  end;<br>
    </p>
    <p>end;</p>
    <p><br>
    </p>
    <p>Nikolay<br>
    </p>
  </body>
</html>