[Nix-dev] Re: Proposal: tryCatch function

Marc Weber marco-oweber at gmx.de
Tue Jan 6 12:55:38 CET 2009


hi @ll

can we focus on the problem again?

Why do we need try catch at all?

I guess the only reason is getting nicer error messages.

If there are errors, failing assertion etc I don't care which one I
get.. I only care about information which helps me to solve the issue.
So what throw "a" + throw "b" should return? It doesn't really matter
unless you start writing recovery code missing the other maybe not yet
throw "hurry up: tidy up your room and invite girl-friend first"
excpetion :-)
Maybe that's why we haven't had any catch in the past. Again if we only
trace some information such as "while evaluating ..." and rethrow the
exception it still doesn't matter. At least one failure should make the
derivation fail.

Let's talk about an example illustrating the problems:

  Example1:
  let enclosedExcpetion = catch ( __add 1 (if (__getenv "FOO" == "FOO" then throw "FOO is FOO" else 1) ) )
      addTwo = x : x + 2
  in addTwo enclosedExcpetion

Now what happens?

addTwo enclosedExcpetion
gets
  enclosedExcpetion + 2 
gets
  catch ( __add ... ) + 2
gets ( due to lazy evaluation!)
  __add 1 (if ..  ) + 2

now our catch is gone the exception will still be thrown..

So maybe we only need one of the following

  traceOnExcpetionAndRethrow expr "trace message"
  replaceExcpetion expr (oldException: throw "new ecpetion")

this way we can ensure that a failure never vanish and cannot be caught ?

Looking at the example above again and modifying it slightly you could
write this now:

  Example 2:
  let enclosedExcpetion = catch ( __add 1 (if (__getenv "FOO" == "FOO" then throw "FOO is FOO" else 1) ) )
      addTwo = replaceExcpetion (x : x + 2) (ex : "while evaluating addTwo ${__toString ex}" )
  in addTwo enclosedExcpetion

Where is the difference?

If you exeucte Example1 you'll get
  exception "FOO == FOO"

executing the second example you'll get
  "while evaluating addTwo exception \"FOO == FOO\""

And the second case does give you valuable information because the "FOO
== FOO" exception doesn't tell you about where it has been evaluated?

Sincerly
Marc Weber



More information about the nix-dev mailing list