octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

unwind_protect & try/catch combined corner case


From: Jaroslav Hajek
Subject: unwind_protect & try/catch combined corner case
Date: Thu, 7 Jan 2010 09:46:00 +0100

hi all,

consider this script which mixes unwind_protect and try/catch in a
not-really-neat way:
unwind_protect
  try
    unwind_protect
      fdisp (stderr, "press Ctrl-C");
      pause (3)
      #for i = 1:100000
      #  fprintf (stderr, "\r%d", i);
      #endfor
    unwind_protect_cleanup
      fprintf (stderr, "\n");
      fdisp (stderr, "cleanup inner");
      error ("raise error");
    end_unwind_protect
  catch
    fdisp (stderr, "caught error");
  end_try_catch
  fdisp (stderr, "intermediate");
unwind_protect_cleanup
  fdisp (stderr, "cleanup outer");
end_unwind_protect
fdisp (stderr, "outer normal");

what should it do? With my recent rewrite of unwind_protect and
consequent changes, this works like this:

octave:1> tea1
press Ctrl-C
^C
cleanup inner
cleanup outer

octave:1>

formerly (and in 3.2.x as well), the behavior was:

octave:1> tea1
press Ctrl-C
^C
cleanup inner
caught error
intermediate
cleanup outer
outer normal
octave:2>

I think the latter is plain wrong; the interrupt is somehow swallowed
by the try/catch block
(note that this doesn't happen normally, just when unwind_protect is
embedded in try/catch).

But is the former correct? I believe it's the most reasonable behavior:
the "try" section is entered, then unwind_protect. An interrupt occurs
there, hence control is transferred to unwind_protect_cleanup.
unwind_protect_cleanup executes some code which finally raises an
error; however, this error is suppressed because we're in a "try"
section.
The unwind_protect_cleanup section is exited. Since there is still an
interrupt pending, the interrupt handling is resumed and the "catch"
section is not executed (because catch cannot handle interrupts);
instead, the interrupt is propagated upwards; in this case, to the
outer unwind_protect block which again correctly executes the cleanup
section, but eventually the interrupt reaches the top level.

Comments? Different opinions? Matlab doesn't have unwind_protect, so
it won't help here.

Note that this is really a strange corner case because it makes no
sense to handle errors in unwind_protect_cleanup section like this;
indeed, if the cleanup is expected to possibly raise an error, the
try/catch block should be embedded inside it.

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]