Glenn Linderman wrote:
> 
> So that was one thing I tried to do in RFC 119: it discusses
> only throwing and handling exceptions.  It leaves open what is
> thrown.  It allows for simple exceptions in simple programs to
> use simple mechanisms (throw a scalar); it allows for complex
> but non-OO exceptions to be thrown as a list; it allows for
> complex OO-type (C++/Java-type) exceptions to be thrown as an
> object reference.

RFC 88 v1 does that too.  RFC 88 v2 still proposes a non-OO (ie,
procedural) exception handling mechanism, it just expects actual
exceptions to be objects, and defines the details thereto.  RFC
88 still lets you C<die "Can't foo.">, it just wraps up "Can't
foo." into a canonical Exception object before setting $@, such
that $@->{message} eq "Can't foo.".  This can, of course, be
completely ignored, since Exception object stringification
basically returns the value of the message instance variable.
So after C<die "Can't foo.">, C<print "Trouble: $@\n"> still
works as it did in Perl 5.

> RFC 119 also addresses one feature not found in any of the OO
> exception mechanism RFCs, including the "unified" one which
> has ignored RFC 119 so far: bundling the "cleanup" code with
> the "setup" code.  I find the syntax proposed there an
> extremely handy way of dealing with cleanup code.
> 
>      $fh = open ( ... ) except close ( $fh );
> 
> is an extremely concise way of expressing the cleanup code
> corresponding with the setup code at the point in which the
> mental context is focused on the need to open the file.  In
> version 2 of RFC 119, I will be adding a variation to this:
> the finally keyword can be used in place of except to indicate
> code that should be executed not only if an exception is
> thrown by but also when the scope exits normally.  Hence, at
> the point at which you open a local file in a routine, you can
> specify its closure via:
> 
>     $fh = open ( ... ) finally close ( $fh );
> 
> This will handle the close of the function regardless of how
> the scope exits: via a return, via falling out the end, or via
> an exception.

I think this is cool.  If you keep the "except" keyword, and
change from "finally" to "always", then both these mechanisms
could be added to Perl 6 exception handling.

> Today I often find myself coding in the following manner:
> 
>    open HANDLE, ...  ||  die ...;
>    while ( <HANDLE> ) {
>    }
>    close ( HANDLE );
> 
> and then going back up and filling in the block.  This way I
> don't forget to close the HANDLE at the appropriate time, and
> as long as I don't return from within the while, all is well.

I often find myself coding in the following manner:

    open F, ... or die;
    try {
        }
    finally { close F; }

and then going back and filling in the try block.  Using this
technique has the advantage of closing F whether or not try
raises an exception.

I've added RFC 119 to RFC 88's REFERENCES, and queued up an
impact statement thereto.

Yours, &c, Tony Olekshy

Reply via email to