[Nix-dev] Re: Proposal: tryCatch function

Nicolas Pierron nicolas.b.pierron at gmail.com
Tue Jan 6 09:56:34 CET 2009


On Tue, Jan 6, 2009 at 09:32, Eelco Dolstra <e.dolstra at tudelft.nl> wrote:
> Hi,
>
> Marc Weber wrote:
>
>>> Another concern is that it catches all exceptions.  Compare this with
>>> what other languages typically provide, where only exceptions of the
>>> specified type are caught (it's also usually possible to catch
>>> exceptions of any type, e.g., by specifying `Exception' as the exception
>>> type in Java, but that has to be made explicit.)
>>
>> We don't have types.. Are you thinking about
>> catchString catchAttrs catchNull catchList  ?
>
> You could throw a value, and rethrow it if you can't handle it.  However, having
> distinct kinds of exceptions is a problem in a purely functional language, since
> the exception that gets thrown might depend on the evaluation order.  This is
> why Haskell only allows exceptions to be caught in the I/O monad.

There throw should not throw an exception in the C++ code but it
should just return a normal form.

throw "foo": is a normal form
(throw "foo") + "bar" : is not a normal form and it returns throw "foo".

This remove the problem of the evaluation order I guess.

There is still another issue which is about tryCatch which is that
even it this evaluation seems correct it does not feel right in your
code when you are returning a function which after cause an exception
when it is called.

  ...
  v7 = tryCatch v6 (msg: builtins.throw "v7: ${msg}");
  v8 = arg: tryCatch (v7 arg) (msg: builtins.throw "v8: ${msg}");
  v9 = tryCatch v8 (msg: builtins.throw "v9: ${msg}");
  ...

The solution would be to add a function like:

functionCatch = value: catch:
  tryCatch (if __isFunction value then functionCatch value catch else
value) catch;

which will add the tryCatch function at all stages of the function computation.

tryCatch is called like that to escape future name collisions when the
sugar will be introduced  "try ... catch ...".

-- 
Nicolas Pierron
http://www.linkedin.com/in/nicolasbpierron
- If you are doing something twice then you should try to do it once.



More information about the nix-dev mailing list