[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: thoughts about unwind_protect
From: |
dbateman |
Subject: |
Re: thoughts about unwind_protect |
Date: |
Tue, 5 Jan 2010 04:57:27 -0800 (PST) |
Robert T. Short wrote:
>
> Sounds more like an argument against global variables than an argument
> for unwind_protect.
>
> I suppose I should use this feature a bit just to understand. Probably
> won't though.
>
> Bob
>
>
> David Bateman wrote:
>> Robert T. Short wrote:
>>> I have a general question about unwind_protect.
>>>
>>> Would someone take the time to explain the advantages of
>>> unwind_protect over try/catch?
>>>
>> From the FAQ
>>
>> Octave has a lisp like unwind_protect block that allows blocks of
>> code that terminate in an error to ensure that the variables that
>> are touched are restored. You can do something similar with
>> @code{try}/@code{catch} combined with @samp{rethrow (lasterror ())} in
>> @sc{Matlab}, however rethrow and lasterror are only available in
>> Octave 2.9.10 and later.
>>
>> Note that using @code{try}/@code{catch} combined with @samp{rethrow
>> (lasterror ())} can not guarantee that global variables will be
>> correctly reset, as it won't catch user interrupts with Ctrl-C. For
>> example
>>
>> @example
>> @group
>> global a
>> a = 1;
>> try
>> _a = a;
>> a = 2
>> while true
>> end
>> catch
>> fprintf ('caught interrupt\n');
>> a = _a;
>> rethrow (lasterror());
>> end
>> @end group
>> @end example
>>
>> @noindent
>> compared to
>>
>> @example
>> @group
>> global a
>> a = 1;
>> unwind_protect
>> _a = a;
>> a = 2
>> while true
>> end
>> unwind_protect_cleanup
>> fprintf ('caught interrupt\n');
>> a = _a;
>> end
>> @end group
>> @end example
>>
>> Typing Ctrl-C in the first case returns the user directly to the
>> prompt, and the variable "a" is not reset to the saved value. In the
>> second case the variable "a" is reset correctly. Therefore @sc{Matlab}
>> gives no save way of temporarily changing global variables.
>>
>>
>
>
>
Ok then consider
saved_path = pwd ();
unwind_protect
saved_path = pwd ();
cd ('some_other_directory')
a_function_that_does_something ();
unwind_protect_cleanup
cd (saved_path);
end
Wouldn't you prefer that the user comes back to the same directory after
hitting Ctrl-C?
D.
--
View this message in context:
http://old.nabble.com/thoughts-about-unwind_protect-tp26996316p27026605.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.
- thoughts about unwind_protect, Jaroslav Hajek, 2010/01/02
- Re: thoughts about unwind_protect, Jaroslav Hajek, 2010/01/06
- Re: thoughts about unwind_protect, Marco Atzeri, 2010/01/06
- Re: thoughts about unwind_protect, Jaroslav Hajek, 2010/01/06
- Re: thoughts about unwind_protect, Marco Atzeri, 2010/01/07
- Re: thoughts about unwind_protect, Jaroslav Hajek, 2010/01/07
- Re: thoughts about unwind_protect, Marco Atzeri, 2010/01/07
- Re: thoughts about unwind_protect, Jaroslav Hajek, 2010/01/07
- Re: thoughts about unwind_protect, Jaroslav Hajek, 2010/01/07
- Re: thoughts about unwind_protect, Marco Atzeri, 2010/01/07