Module name:  try-catch
https://pkgd.racket-lang.org/pkgn/package/try-catch

Lisp stereotypically has a problem that it's super easy to write your own
code so the library ecosystem ends up fractured, with multiple options to
do the same thing.  McCarthy forbid that I should break the stereotype, so
here's my entry into the try/catch niche.

> (try [shared (define username "bob")]
       [pre (printf "in pre, prepping to handle ~a.\n" username)]
       [(printf "in body. hello, ~a.\n" username)]
       [post (printf "in post, goodbye ~a.\n" username)]
       [catch (symbol? (printf "the symbol was ~a\n" e))]
       [cleanup (printf "in cleanup, done with ~a." username)])
in pre, prepping to handle bob.
in body. hello, bob.
in post, goodbye bob.
in cleanup, done with bob.


pre/body/post are plugged into a dynamic-wind, meaning that the pre clause
is executed before body whenever control enters the body (either normally
or through a continuation jump or etc) and the post clause is executed
whenever control leaves the body.

The catch clause contains subclauses of (predicate handler-expr) that get
fed into a with-handlers, except the handler-exprs are wrapped in a (lambda
(e) ...) in order to reduce boilerplate.  The value 'e' is available to the
handler-expr.

The shared clause does setup before the pre/body are called and the code in
that clause is visible to all subsequent clauses.

The cleanup clause is run iff the body exits without error.

See the documentation for full details and examples.  (Note that the
package server is not currently admitting that there is documentation but
there will be when you install it.)

Competing options:

try  (Typed Racket):  https://pkgs.racket-lang.org/package/try
try-catch-finally and try-catch-finally-lib:
https://pkgs.racket-lang.org/package/try-catch-finally and
https://pkgs.racket-lang.org/package/try-catch-finally-lib
try-catch-match: https://pkgs.racket-lang.org/package/try-catch-match
try-make-sarna-happy:
https://pkgs.racket-lang.org/package/try-make-sarna-happy

My motivation for writing this instead of submitting pull requests was in
part syntactic (the other libraries all use (catch ...) for each of the
catch clauses and I dispreferred the redundancy of typing 'catch' over and
over) and in part because I wanted the 'shared' and 'cleanup' phases which
would have required greater changes to their macros than I was comfortable
submitting.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKoeQXhhKatZzjMePd_qbqiNPBOeyX%3DsMqisAVateOBVKrQ%40mail.gmail.com.

Reply via email to