comparison lispref/control.texi @ 21682:90da2489c498

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Mon, 20 Apr 1998 17:43:57 +0000
parents 66d807bdc5b4
children d4ac295a98b3
comparison
equal deleted inserted replaced
21681:11eafe90b842 21682:90da2489c498
458 @end group 458 @end group
459 @end example 459 @end example
460 460
461 @noindent 461 @noindent
462 This moves forward one line and continues moving by lines until it 462 This moves forward one line and continues moving by lines until it
463 reaches an empty. It is unusual in that the @code{while} has no body, 463 reaches an empty line. It is peculiar in that the @code{while} has no
464 just the end test (which also does the real work of moving point). 464 body, just the end test (which also does the real work of moving point).
465 @end defspec 465 @end defspec
466 466
467 @node Nonlocal Exits 467 @node Nonlocal Exits
468 @section Nonlocal Exits 468 @section Nonlocal Exits
469 @cindex nonlocal exits 469 @cindex nonlocal exits
716 716
717 These examples show typical uses of @code{error}: 717 These examples show typical uses of @code{error}:
718 718
719 @example 719 @example
720 @group 720 @group
721 (error "You have committed an error. 721 (error "That is an error -- try something else")
722 Try something else.") 722 @error{} That is an error -- try something else
723 @error{} You have committed an error. 723 @end group
724 Try something else. 724
725 @end group 725 @group
726 726 (error "You have committed %d errors" 10)
727 @group 727 @error{} You have committed 10 errors
728 (error "You have committed %d errors." 10)
729 @error{} You have committed 10 errors.
730 @end group 728 @end group
731 @end example 729 @end example
732 730
733 @code{error} works by calling @code{signal} with two arguments: the 731 @code{error} works by calling @code{signal} with two arguments: the
734 error symbol @code{error}, and a list containing the string returned by 732 error symbol @code{error}, and a list containing the string returned by
750 condition names. This is how Emacs Lisp classifies different sorts of 748 condition names. This is how Emacs Lisp classifies different sorts of
751 errors. 749 errors.
752 750
753 The number and significance of the objects in @var{data} depends on 751 The number and significance of the objects in @var{data} depends on
754 @var{error-symbol}. For example, with a @code{wrong-type-arg} error, 752 @var{error-symbol}. For example, with a @code{wrong-type-arg} error,
755 there are two objects in the list: a predicate that describes the type 753 there should be two objects in the list: a predicate that describes the type
756 that was expected, and the object that failed to fit that type. 754 that was expected, and the object that failed to fit that type.
757 @xref{Error Symbols}, for a description of error symbols. 755 @xref{Error Symbols}, for a description of error symbols.
758 756
759 Both @var{error-symbol} and @var{data} are available to any error 757 Both @var{error-symbol} and @var{data} are available to any error
760 handlers that handle the error: @code{condition-case} binds a local 758 handlers that handle the error: @code{condition-case} binds a local
770 (signal 'wrong-number-of-arguments '(x y)) 768 (signal 'wrong-number-of-arguments '(x y))
771 @error{} Wrong number of arguments: x, y 769 @error{} Wrong number of arguments: x, y
772 @end group 770 @end group
773 771
774 @group 772 @group
775 (signal 'no-such-error '("My unknown error condition.")) 773 (signal 'no-such-error '("My unknown error condition"))
776 @error{} peculiar error: "My unknown error condition." 774 @error{} peculiar error: "My unknown error condition"
777 @end group 775 @end group
778 @end smallexample 776 @end smallexample
779 @end defun 777 @end defun
780 778
781 @cindex CL note---no continuable errors 779 @cindex CL note---no continuable errors
874 completely before execution of the handler, the handler cannot resume 872 completely before execution of the handler, the handler cannot resume
875 execution at the point of the error, nor can it examine variable 873 execution at the point of the error, nor can it examine variable
876 bindings that were made within the protected form. All it can do is 874 bindings that were made within the protected form. All it can do is
877 clean up and proceed. 875 clean up and proceed.
878 876
879 @code{condition-case} is often used to trap errors that are 877 The @code{condition-case} construct is often used to trap errors that
880 predictable, such as failure to open a file in a call to 878 are predictable, such as failure to open a file in a call to
881 @code{insert-file-contents}. It is also used to trap errors that are 879 @code{insert-file-contents}. It is also used to trap errors that are
882 totally unpredictable, such as when the program evaluates an expression 880 totally unpredictable, such as when the program evaluates an expression
883 read from the user. 881 read from the user.
884 882
885 Error signaling and handling have some resemblance to @code{throw} and 883 Error signaling and handling have some resemblance to @code{throw} and
1157 You might think that we could just as well write @code{(kill-buffer 1155 You might think that we could just as well write @code{(kill-buffer
1158 (current-buffer))} and dispense with the variable @code{buffer}. 1156 (current-buffer))} and dispense with the variable @code{buffer}.
1159 However, the way shown above is safer, if @var{body} happens to get an 1157 However, the way shown above is safer, if @var{body} happens to get an
1160 error after switching to a different buffer! (Alternatively, you could 1158 error after switching to a different buffer! (Alternatively, you could
1161 write another @code{save-excursion} around the body, to ensure that the 1159 write another @code{save-excursion} around the body, to ensure that the
1162 temporary buffer becomes current in time to kill it.) 1160 temporary buffer becomes current again in time to kill it.)
1161
1162 Emacs includes a standard macro called @code{with-temp-buffer} which
1163 expands into more or less the code shown above (@pxref{Current Buffer}).
1164 Several of the macros defined in this manual use @code{unwind-protect}
1165 in this way.
1163 1166
1164 @findex ftp-login 1167 @findex ftp-login
1165 Here is an actual example taken from the file @file{ftp.el}. It 1168 Here is an actual example taken from the file @file{ftp.el}. It
1166 creates a process (@pxref{Processes}) to try to establish a connection 1169 creates a process (@pxref{Processes}) to try to establish a connection
1167 to a remote machine. As the function @code{ftp-login} is highly 1170 to a remote machine. As the function @code{ftp-login} is highly
1186 This example actually has a small bug: if the user types @kbd{C-g} to 1189 This example actually has a small bug: if the user types @kbd{C-g} to
1187 quit, and the quit happens immediately after the function 1190 quit, and the quit happens immediately after the function
1188 @code{ftp-setup-buffer} returns but before the variable @code{process} is 1191 @code{ftp-setup-buffer} returns but before the variable @code{process} is
1189 set, the process will not be killed. There is no easy way to fix this bug, 1192 set, the process will not be killed. There is no easy way to fix this bug,
1190 but at least it is very unlikely. 1193 but at least it is very unlikely.
1191
1192 Here is another example which uses @code{unwind-protect} to make sure
1193 to kill a temporary buffer. In this example, the value returned by
1194 @code{unwind-protect} is used.
1195
1196 @smallexample
1197 (defun shell-command-string (cmd)
1198 "Return the output of the shell command CMD, as a string."
1199 (save-excursion
1200 (set-buffer (generate-new-buffer " OS*cmd"))
1201 (shell-command cmd t)
1202 (unwind-protect
1203 (buffer-string)
1204 (kill-buffer (current-buffer)))))
1205 @end smallexample