{"size": 1896, "ext": "scrbl", "max_stars_repo_path": "try-catch-finally/scribblings/try-catch-finally.scrbl", "max_stars_repo_name": "AlexKnauth/try-catch-finally", "max_stars_repo_stars_event_min_datetime": "2021-07-29T10:21:42.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-13T18:57:03.000Z", "max_issues_repo_path": "try-catch-finally/scribblings/try-catch-finally.scrbl", "max_issues_repo_name": "AlexKnauth/try-catch-finally", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "try-catch-finally/scribblings/try-catch-finally.scrbl", "max_forks_repo_name": "AlexKnauth/try-catch-finally", "max_forks_repo_forks_event_min_datetime": "2021-07-29T12:32:40.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-29T12:32:40.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 32.1355932203, "max_line_length": 74, "alphanum_fraction": 0.5701476793}
#lang scribble/manual

@(require scribble/example
          (for-label try-catch-finally
                     racket/base))

@title{try-catch-finally}
@author{Alex Knauth}

@(define ev (make-base-eval '(require try-catch-finally)))

@defmodule[try-catch-finally]{
A macro for catching exceptions and running teardown operations.
}

@defform[#:literals [catch finally => _]
         (try body ...+ catch-clause ... maybe-finally-clause)
         #:grammar ([catch-clause (catch pred-expr => handler-expr)
                                  (catch (pred-expr id) handler-body ...+)
                                  (catch (id) handler-body ...+)
                                  (catch _ handler-body ...+)]
                    [maybe-finally-clause (code:line)
                                          (finally post-body ...+)])]{
After evaluating the @racket[pred-expr]s and @racket[handler-expr]s,
@racket[try] evaluates the @racket[body]s with the new exception handlers,
and then evaluates the @racket[post-body]s even if execution exits the
@racket[body]s through an exception or continuation.

@examples[#:eval ev
  (try
    (raise-syntax-error #f "a syntax error")
    (catch (exn:fail:syntax? e)
      (displayln "got a syntax error")))
  (try
    (raise-syntax-error #f "a syntax error")
    (catch (exn:fail:syntax? e)
      (displayln "got a syntax error"))
    (catch (exn:fail? e)
      (displayln "fallback clause")))
  (try
    (displayln "at")
    (finally (displayln "out")))
  (let/cc up
    (try
      (displayln "at before")
      (up (void))
      (displayln "at after")
      (finally (displayln "out"))))
]

@defsubform*[#:literals [=> _]
             [(catch pred-expr => handler-expr)
              (catch (pred-expr id) handler-body ...+)
              (catch (id) handler-body ...+)
              (catch _ handler-body ...+)]]

@defsubform[(finally post-body ...+)]
}
