octave-maintainers
[Top][All Lists]
Advanced

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

try/finally


From: Andrew Janke
Subject: try/finally
Date: Wed, 20 Mar 2019 18:39:17 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.5.3

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 via email to

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