try/finally

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

try/finally

apjanke-floss
Hi, Octave maintainers,

What do you think of adding a "finally" clause to the "try" block syntax?

Using unwind_protect in conjunction with try/catch is somewhat cumbersome:

unwind_protect
     try
         % ... main code ...
     catch
         % ... error handling ...
     end_try_catch
unwind_protect_cleanup
     % ... unconditional cleanup ...
end_unwind_protect

A try/catch/finally would have exactly the same semantics as the above,
but would be more concise (just syntactic sugar):

try
   % ... main code ...
catch
   % ... error handling ...
finally
    % ... unconditional cleanup ...
end_try_catch

That seems like cleaner code to me.

It might be easier for new users to learn, too: "unwind protect" is a
technical, computer science-y term that might be intimidating to newer
programmers. And many of the other languages that they already know –
Java, C#, Python, R, Julia, Maple, JavaScript, Ruby – all use "finally".
"unwind-protect" seems to be a Lisp-ism, and I don't think we have many
Lisp coders coming to Octave.

Matlab has neither try/catch nor unwind_protect, so compatibility is not
an immediate concern. And I think they're more likely to add a finally
than an unwind_protect.

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: try/finally

John W. Eaton
Administrator
On 3/20/19 5:39 PM, Andrew Janke wrote:

> Matlab has neither try/catch nor unwind_protect, so compatibility is not
> an immediate concern. And I think they're more likely to add a finally
> than an unwind_protect.
Matlab does have try/catch:

   https://www.mathworks.com/help/matlab/ref/try.html

It would still be possible to add a finally block, but I'm not too
interested in language extensions these days since they tend to create
future compatibility issues.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: try/finally

apjanke-floss


On 3/20/19 10:32 PM, John W. Eaton wrote:
> On 3/20/19 5:39 PM, Andrew Janke wrote:
>
>> Matlab has neither try/catch nor unwind_protect, so compatibility is
>> not an immediate concern. And I think they're more likely to add a
>> finally than an unwind_protect.
> Matlab does have try/catch:
>
>    https://www.mathworks.com/help/matlab/ref/try.html

Sorry, I meant "Matlab doesn't have try/finally".

> It would still be possible to add a finally block, but I'm not too
> interested in language extensions these days since they tend to create
> future compatibility issues.

Okay.

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: try/finally

Mike Miller-4
On Wed, Mar 20, 2019 at 22:42:40 -0400, Andrew Janke wrote:

> On 3/20/19 10:32 PM, John W. Eaton wrote:
> > On 3/20/19 5:39 PM, Andrew Janke wrote:
> >
> > > Matlab has neither try/catch nor unwind_protect, so compatibility is
> > > not an immediate concern. And I think they're more likely to add a
> > > finally than an unwind_protect.
> > Matlab does have try/catch:
> >
> >    https://www.mathworks.com/help/matlab/ref/try.html
>
> Sorry, I meant "Matlab doesn't have try/finally".

I know it's kind of like reading backwards, but using onCleanup might be
a simpler solution than nesting a try-catch inside of an unwind_protect:

    finally = onCleanup (@() … clean up or restore something … );
    try
      ##  main code
    catch
      ##  handle errors
    end_try_catch
    clear finally   ## force immediate execution? or just return

Doesn't work that well if your finally needs access to local variables
that may be changing inside the try block, but may be useful.

--
mike

Reply | Threaded
Open this post in threaded view
|

Re: try/finally

apjanke-floss


On 3/21/19 1:01 AM, Mike Miller wrote:

> On Wed, Mar 20, 2019 at 22:42:40 -0400, Andrew Janke wrote:
>> On 3/20/19 10:32 PM, John W. Eaton wrote:
>>> On 3/20/19 5:39 PM, Andrew Janke wrote:
>>>
>>>> Matlab has neither try/catch nor unwind_protect, so compatibility is
>>>> not an immediate concern. And I think they're more likely to add a
>>>> finally than an unwind_protect.
>>> Matlab does have try/catch:
>>>
>>>     https://www.mathworks.com/help/matlab/ref/try.html
>>
>> Sorry, I meant "Matlab doesn't have try/finally".
>
> I know it's kind of like reading backwards, but using onCleanup might be
> a simpler solution than nesting a try-catch inside of an unwind_protect:
>
>      finally = onCleanup (@() … clean up or restore something … );
>      try
>        ##  main code
>      catch
>        ##  handle errors
>      end_try_catch
>      clear finally   ## force immediate execution? or just return
>
> Doesn't work that well if your finally needs access to local variables
> that may be changing inside the try block, but may be useful.
>

That makes sense. I hadn't thought of doing an explicit clear on the
cleanup variable prior to function return. Thanks.

Andrew