Negative
Sean Kelly
sean at f4.ca
Wed Mar 1 09:43:01 PST 2006
Walter Bright wrote:
> "Andrew Fedoniouk" <news at terrainformatica.com> wrote in message
> news:du2q1c$4tv$1 at digitaldaemon.com...
>> "Walter Bright" <newshound at digitalmars.com> wrote in message
>>> The new bug in the above code is that the catch block fails to rethrow o.
>> Let's say it was an intention. Pretty common by the way.
>
> Perhaps, but the stated point of the example was to show equivalent
> try-catch code to the on_scope example. It isn't equivalent.
That may have been the point of the original example, but it wasn't the
point of mine. But it's water under the bridge, as you answered my
question either way :-)
>> Having on_scope_*** spread all other the "... lots of code ..." will
>> create
>> code maintainance nightmare as to trace visually what will happen and when
>> will not be a task for human anymore.
>
> I submit that based on what's been posted in this thread, it's easier to get
> on_scope *correct* than try-finally, because most of the try-finally
> examples posted here do not work as intended by the author. Not only that,
> the try-finally examples fail in ways that are difficult to write test cases
> for, so the errors are likely to go unnoticed.
The only complaint I've heard about on_scope that I consider valid is
that program flow may be a tad confusing in excessively long functions.
But traditional RAII is no better, and try-finally introduces
maintenance and readability problems by breaking the logical connection
between unwinding code and the code block it's associated with. It's
also helpful that throwing an exception while another is in-flight in D
does not result in program termination, even if doing so is not advisable.
>> And as far as I understand main idea of on_scope_exit is a sort of poor
>> man struct destructor...
>> I suspect that you are thinking about how to remove 'auto'/RAII, right?
>
> No. RAII is for managing resources, which is different from managing state
> or transactions. try-catch is still needed, as on_scope doesn't catch
> exceptions. It's try-finally that becomes redundant, though it is useful to
> keep it because so many people are used to it.
That may have been the original intent, but RAII has since become almost
indispensable for writing exception-safe code, be it with resources,
transactions, or something else. Personally, I've never liked
try-finally, but I attribute that to my C++ background. If I were a
Java person it may be a different story.
Sean
More information about the Digitalmars-d
mailing list