unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 68075@debbugs.gnu.org
Subject: bug#68075: 30.0.50; New special form `handler-bind`
Date: Thu, 28 Dec 2023 13:14:25 -0500	[thread overview]
Message-ID: <jwvzfxudwl6.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <jwv5y0ifgcq.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Thu, 28 Dec 2023 13:12:12 -0500")

> See patch below for my attempt to clarify.

Sigh!

Here it is,


        Stefan


diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi
index 4107963eed5..e63660206d0 100644
--- a/doc/lispref/control.texi
+++ b/doc/lispref/control.texi
@@ -2311,24 +2311,100 @@ Handling Errors
 @code{(@var{conditions} @var{handler})} where @var{conditions} is an
 error condition name to be handled, or a list of condition names, and
 @var{handler} should be a form whose evaluation should return a function.
+As with @code{condition-case}, condition names are symbols.
 
 Before running @var{body}, @code{handler-bind} evaluates all the
 @var{handler} forms and installs those handlers to be active during
-the evaluation of @var{body}.  These handlers are searched together
-with those installed by @code{condition-case}.  When the innermost
+the evaluation of @var{body}.  When an error is signaled,
+Emacs searches all the active @code{condition-case} and
+@code{handler-bind} forms for a handler that
+specifies one or more of these condition names.  When the innermost
 matching handler is one installed by @code{handler-bind}, the
 @var{handler} function is called with a single argument holding the
 error description.
 
-@var{handler} is called in the dynamic context where the error
-happened, without first unwinding the stack, meaning that all the
-dynamic bindings are still in effect, except that all the error
-handlers between the code that signaled the error and the
-@code{handler-bind} are temporarily suspended.  Like any normal
-function, @var{handler} can exit non-locally, typically via
-@code{throw}, or it can return normally.  If @var{handler} returns
-normally, it means the handler @emph{declined} to handle the error and
-the search for an error handler is continued where it left off.
+Contrary to what happens with @code{condition-case}, @var{handler} is
+called in the dynamic context where the error happened, without
+unbinding any variable bindings or running any cleanups of
+@code{unwind-protect}, so that all those dynamic bindings are still in
+effect.  There is one exception: while running the @var{handler}
+function, all the error handlers between the code that signaled the
+error and the @code{handler-bind} are temporarily suspended, meaning
+that when an error is signaled, Emacs will only search the active
+@code{condition-case} and @code{handler-bind} forms that are inside
+the @var{handler} function or outside of the current
+@code{handler-bind}.
+
+Like any normal function, @var{handler} can exit non-locally,
+typically via @code{throw}, or it can return normally.
+If @var{handler} returns normally, it means the handler
+@emph{declined} to handle the error and the search for an error
+handler is continued where it left off.
+
+For example, if we wanted to keep a log of all the errors that occur
+during the execution of a particular piece of code together with the
+buffer that's current when the error is signaled, but without
+otherwise affecting the behavior of that code, we can do it with:
+
+@example
+@group
+(handler-bind
+    ((error
+      (lambda (err)
+        (push (cons err (current-buffer)) my-log-of-errors))))
+  @var{body-forms}@dots{})
+@end group
+@end example
+
+This will log only those errors that are not caught internally to
+@var{body-forms}@dots{}, in other words errors that ``escape'' from
+@var{body-forms}@dots{}, and it will not prevent those errors from
+being passed on to surrounding @code{condition-case} handlers (or
+@code{handler-bind} handlers for that matter) since the above handler
+returns normally.
+
+We can also use @code{handler-bind} to replace an error with another,
+as in the code below which turns all errors of type @code{user-error}
+that occur during the execution of @var{body-forms}@dots{} into plain
+@code{error}:
+
+@example
+@group
+(handler-bind
+    ((user-error
+      (lambda (err)
+        (signal 'error (cdr err)))))
+  @var{body-forms}@dots{})
+@end group
+@end example
+
+We can get almost the same result with @code{condition-case}:
+
+@example
+@group
+(condition-case err
+    (progn @var{body-forms}@dots{})
+  (user-error (signal 'error (cdr err))))
+@end group
+@end example
+
+But with the difference that when we (re)signal the new error in
+@code{handler-bind} the dynamic environment from the original error is
+still active, which means for example that if we were to enter the
+debugger at this point, it will show us a complete backtrace including
+the point where we signaled the original error:
+
+@example
+@group
+Debugger entered--Lisp error: (error "Oops")
+  signal(error ("Oops"))
+  (closure (t) (err) (signal 'error (cdr err)))((user-error "Oops"))
+  user-error("Oops")
+  @dots{}
+  eval((handler-bind ((user-error (lambda (err) @dots{}
+@end group
+@end example
+
 @end defmac
 
 @node Error Symbols
diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi
index 45c892ad5a7..c3d980e1717 100644
--- a/doc/lispref/eval.texi
+++ b/doc/lispref/eval.texi
@@ -848,15 +848,17 @@ Eval
 @end defopt
 
 @defopt lisp-eval-depth-reserve
-In order to be able to debug infinite recursion errors, Entry to the
-Lisp debugger increases temporarily the value of
+In order to be able to debug infinite recursion errors, when invoking the
+Lisp debugger, Emacs increases temporarily the value of
 @code{max-lisp-eval-depth}, if there is little room left, to make sure
 the debugger itself has room to execute.  The same happens when
-running the handler of a @code{handler-bind}.
+running the handler of a @code{handler-bind}.  @xref{Handling Errors}.
 
 The variable @code{lisp-eval-depth-reserve} bounds the extra depth
 that Emacs can add to @code{max-lisp-eval-depth} for those
 exceptional circumstances.
+
+The default value of this variable is 200.
 @end defopt
 
 
diff --git a/etc/NEWS b/etc/NEWS
index 014c32b4d8e..e446f05190a 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1371,6 +1371,7 @@ It puts a limit to the amount by which Emacs can temporarily increase
 Provides a functionality similar to `condition-case` except it runs the
 handler code without unwinding the stack, such that we can record the
 backtrace and other dynamic state at the point of the error.
+See the Info node "(elisp) Handling Errors".
 
 +++
 ** New 'pop-up-frames' action alist entry for 'display-buffer'.
diff --git a/lisp/subr.el b/lisp/subr.el
index 600b4d27f18..57e2473c2bb 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -7501,13 +7501,13 @@ handler-bind
   "Setup error HANDLERS around execution of BODY.
 HANDLERS is a list of (CONDITIONS HANDLER) where
 CONDITIONS should be a list of condition names (symbols) or
-a single condition name and HANDLER is a form whose evaluation
+a single condition name, and HANDLER is a form whose evaluation
 returns a function.
 When an error is signaled during execution of BODY, if that
 error matches CONDITIONS, then the associated HANDLER
-function is called with the error as argument.
+function is called with the error object as argument.
 HANDLERs can either transfer the control via a non-local exit,
-or return normally.  If they return normally the search for an
+or return normally.  If a handler returns normally, the search for an
 error handler continues from where it left off."
   ;; FIXME: Completion support as in `condition-case'?
   (declare (indent 1) (debug ((&rest (sexp form)) body)))






  reply	other threads:[~2023-12-28 18:14 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-28  6:33 bug#68075: 30.0.50; New special form `handler-bind` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-28  8:03 ` Eli Zaretskii
2023-12-28 18:12   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-28 18:14     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-12-30  7:50       ` Eli Zaretskii
2023-12-28 18:25     ` Ihor Radchenko
2023-12-28 22:04       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-30  7:52     ` Eli Zaretskii
2023-12-31 19:07       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-31 19:36         ` Eli Zaretskii
2024-01-01 15:37           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-01 16:06             ` Eli Zaretskii
2024-01-01 16:55               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-01 17:35                 ` Eli Zaretskii
2024-01-02  4:56                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-04 23:57                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-05  0:38                       ` Stefan Kangas
2024-01-05  0:50                     ` Stefan Kangas
2024-01-05  2:57                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvzfxudwl6.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=68075@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).