Mercurial > hg > xemacs-beta
comparison man/lispref/control.texi @ 444:576fb035e263 r21-2-37
Import from CVS: tag r21-2-37
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:36:19 +0200 |
parents | 74fd4e045ea6 |
children | 37e56e920ac5 |
comparison
equal
deleted
inserted
replaced
443:a8296e22da4e | 444:576fb035e263 |
---|---|
1 @c -*-texinfo-*- | 1 @c -*-texinfo-*- |
2 @c This is part of the XEmacs Lisp Reference Manual. | 2 @c This is part of the XEmacs Lisp Reference Manual. |
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. | 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
4 @c See the file lispref.texi for copying conditions. | 4 @c See the file lispref.texi for copying conditions. |
5 @setfilename ../../info/control.info | 5 @setfilename ../../info/control.info |
6 @node Control Structures, Variables, Evaluation, Top | 6 @node Control Structures, Variables, Evaluation, Top |
7 @chapter Control Structures | 7 @chapter Control Structures |
8 @cindex special forms for control structures | 8 @cindex special forms for control structures |
150 @code{if} chooses between the @var{then-form} and the @var{else-forms} | 150 @code{if} chooses between the @var{then-form} and the @var{else-forms} |
151 based on the value of @var{condition}. If the evaluated @var{condition} is | 151 based on the value of @var{condition}. If the evaluated @var{condition} is |
152 non-@code{nil}, @var{then-form} is evaluated and the result returned. | 152 non-@code{nil}, @var{then-form} is evaluated and the result returned. |
153 Otherwise, the @var{else-forms} are evaluated in textual order, and the | 153 Otherwise, the @var{else-forms} are evaluated in textual order, and the |
154 value of the last one is returned. (The @var{else} part of @code{if} is | 154 value of the last one is returned. (The @var{else} part of @code{if} is |
155 an example of an implicit @code{progn}. @xref{Sequencing}.) | 155 an example of an implicit @code{progn}. @xref{Sequencing}.) |
156 | 156 |
157 If @var{condition} has the value @code{nil}, and no @var{else-forms} are | 157 If @var{condition} has the value @code{nil}, and no @var{else-forms} are |
158 given, @code{if} returns @code{nil}. | 158 given, @code{if} returns @code{nil}. |
159 | 159 |
160 @code{if} is a special form because the branch that is not selected is | 160 @code{if} is a special form because the branch that is not selected is |
161 never evaluated---it is ignored. Thus, in the example below, | 161 never evaluated---it is ignored. Thus, in the example below, |
162 @code{true} is not printed because @code{print} is never called. | 162 @code{true} is not printed because @code{print} is never called. |
163 | 163 |
164 @example | 164 @example |
165 @group | 165 @group |
166 (if nil | 166 (if nil |
167 (print 'true) | 167 (print 'true) |
168 'very-false) | 168 'very-false) |
169 @result{} very-false | 169 @result{} very-false |
170 @end group | 170 @end group |
171 @end example | 171 @end example |
172 @end defspec | 172 @end defspec |
224 @var{condition} of the last clause, like this: @code{(t | 224 @var{condition} of the last clause, like this: @code{(t |
225 @var{body-forms})}. The form @code{t} evaluates to @code{t}, which is | 225 @var{body-forms})}. The form @code{t} evaluates to @code{t}, which is |
226 never @code{nil}, so this clause never fails, provided the @code{cond} | 226 never @code{nil}, so this clause never fails, provided the @code{cond} |
227 gets to it at all. | 227 gets to it at all. |
228 | 228 |
229 For example, | 229 For example, |
230 | 230 |
231 @example | 231 @example |
232 @group | 232 @group |
233 (cond ((eq a 'hack) 'foo) | 233 (cond ((eq a 'hack) 'foo) |
234 (t "default")) | 234 (t "default")) |
358 You could almost write @code{or} in terms of @code{if}, but not quite: | 358 You could almost write @code{or} in terms of @code{if}, but not quite: |
359 | 359 |
360 @example | 360 @example |
361 @group | 361 @group |
362 (if @var{arg1} @var{arg1} | 362 (if @var{arg1} @var{arg1} |
363 (if @var{arg2} @var{arg2} | 363 (if @var{arg2} @var{arg2} |
364 @var{arg3})) | 364 @var{arg3})) |
365 @end group | 365 @end group |
366 @end example | 366 @end example |
367 | 367 |
368 @noindent | 368 @noindent |
579 (throw 'hack 'yes))) | 579 (throw 'hack 'yes))) |
580 @result{} catch2 | 580 @result{} catch2 |
581 @end group | 581 @end group |
582 | 582 |
583 @group | 583 @group |
584 (catch 'hack | 584 (catch 'hack |
585 (print (catch2 'hack)) | 585 (print (catch2 'hack)) |
586 'no) | 586 'no) |
587 @print{} yes | 587 @print{} yes |
588 @result{} no | 588 @result{} no |
589 @end group | 589 @end group |
662 which you call for other purposes, such as if you try to take the | 662 which you call for other purposes, such as if you try to take the |
663 @sc{car} of an integer or move forward a character at the end of the | 663 @sc{car} of an integer or move forward a character at the end of the |
664 buffer; you can also signal errors explicitly with the functions | 664 buffer; you can also signal errors explicitly with the functions |
665 @code{error}, @code{signal}, and others. | 665 @code{error}, @code{signal}, and others. |
666 | 666 |
667 Quitting, which happens when the user types @kbd{C-g}, is not | 667 Quitting, which happens when the user types @kbd{C-g}, is not |
668 considered an error, but it is handled almost like an error. | 668 considered an error, but it is handled almost like an error. |
669 @xref{Quitting}. | 669 @xref{Quitting}. |
670 | 670 |
671 @defun error format-string &rest args | 671 XEmacs has a rich hierarchy of error symbols predefined via @code{deferror}. |
672 This function signals an error with an error message constructed by | 672 |
673 applying @code{format} (@pxref{String Conversion}) to | 673 @example |
674 @var{format-string} and @var{args}. | 674 error |
675 syntax-error | |
676 invalid-read-syntax | |
677 list-formation-error | |
678 malformed-list | |
679 malformed-property-list | |
680 circular-list | |
681 circular-property-list | |
682 | |
683 invalid-argument | |
684 wrong-type-argument | |
685 args-out-of-range | |
686 wrong-number-of-arguments | |
687 invalid-function | |
688 no-catch | |
689 | |
690 invalid-state | |
691 void-function | |
692 cyclic-function-indirection | |
693 void-variable | |
694 cyclic-variable-indirection | |
695 | |
696 invalid-operation | |
697 invalid-change | |
698 setting-constant | |
699 editing-error | |
700 beginning-of-buffer | |
701 end-of-buffer | |
702 buffer-read-only | |
703 io-error | |
704 end-of-file | |
705 arith-error | |
706 range-error | |
707 domain-error | |
708 singularity-error | |
709 overflow-error | |
710 underflow-error | |
711 @end example | |
712 | |
713 The five most common errors you will probably use or base your new | |
714 errors off of are @code{syntax-error}, @code{invalid-argument}, | |
715 @code{invalid-state}, @code{invalid-operation}, and | |
716 @code{invalid-change}. Note the semantic differences: | |
717 | |
718 @itemize @bullet | |
719 @item | |
720 @code{syntax-error} is for errors in complex structures: parsed strings, | |
721 lists, and the like. | |
722 | |
723 @item | |
724 @code{invalid-argument} is for errors in a simple value. Typically, the | |
725 entire value, not just one part of it, is wrong. | |
726 | |
727 @item | |
728 @code{invalid-state} means that some settings have been changed in such | |
729 a way that their current state is unallowable. More and more, code is | |
730 being written more carefully, and catches the error when the settings | |
731 are being changed, rather than afterwards. This leads us to the next | |
732 error: | |
733 | |
734 @item | |
735 @code{invalid-change} means that an attempt is being made to change some | |
736 settings into an invalid state. @code{invalid-change} is a type of | |
737 @code{invalid-operation}. | |
738 | |
739 @item | |
740 @code{invalid-operation} refers to all cases where code is trying to do | |
741 something that's disallowed. This includes file errors, buffer errors | |
742 (e.g. running off the end of a buffer), @code{invalid-change} as just | |
743 mentioned, and arithmetic errors. | |
744 @end itemize | |
745 | |
746 @defun error datum &rest args | |
747 This function signals a non-continuable error. | |
748 | |
749 @var{datum} should normally be an error symbol, i.e. a symbol defined | |
750 using @code{define-error}. @var{args} will be made into a list, and | |
751 @var{datum} and @var{args} passed as the two arguments to @code{signal}, | |
752 the most basic error handling function. | |
675 | 753 |
676 This error is not continuable: you cannot continue execution after the | 754 This error is not continuable: you cannot continue execution after the |
677 error using the debugger @kbd{r} or @kbd{c} commands. If you wish the | 755 error using the debugger @kbd{r} command. See also @code{cerror}. |
678 user to be able to continue execution, use @code{cerror} or | 756 |
679 @code{signal} instead. | 757 The correct semantics of @var{args} varies from error to error, but for |
758 most errors that need to be generated in Lisp code, the first argument | |
759 should be a string describing the *context* of the error (i.e. the exact | |
760 operation being performed and what went wrong), and the remaining | |
761 arguments or \"frobs\" (most often, there is one) specify the offending | |
762 object(s) and/or provide additional details such as the exact error when | |
763 a file error occurred, e.g.: | |
764 | |
765 @itemize @bullet | |
766 @item | |
767 the buffer in which an editing error occurred. | |
768 @item | |
769 an invalid value that was encountered. (In such cases, the string | |
770 should describe the purpose or \"semantics\" of the value [e.g. if the | |
771 value is an argument to a function, the name of the argument; if the value | |
772 is the value corresponding to a keyword, the name of the keyword; if the | |
773 value is supposed to be a list length, say this and say what the purpose | |
774 of the list is; etc.] as well as specifying why the value is invalid, if | |
775 that's not self-evident.) | |
776 @item | |
777 the file in which an error occurred. (In such cases, there should be a | |
778 second frob, probably a string, specifying the exact error that occurred. | |
779 This does not occur in the string that precedes the first frob, because | |
780 that frob describes the exact operation that was happening. | |
781 @end itemize | |
782 | |
783 For historical compatibility, DATUM can also be a string. In this case, | |
784 @var{datum} and @var{args} are passed together as the arguments to | |
785 @code{format}, and then an error is signalled using the error symbol | |
786 @code{error} and formatted string. Although this usage of @code{error} | |
787 is very common, it is deprecated because it totally defeats the purpose | |
788 of having structured errors. There is now a rich set of defined errors | |
789 to use. | |
790 | |
791 See also @code{cerror}, @code{signal}, and @code{signal-error}." | |
680 | 792 |
681 These examples show typical uses of @code{error}: | 793 These examples show typical uses of @code{error}: |
682 | 794 |
683 @example | 795 @example |
684 @group | 796 @group |
685 (error "You have committed an error. | 797 (error 'syntax-error |
798 "Dialog descriptor must supply at least one button" | |
799 descriptor) | |
800 @end group | |
801 | |
802 @group | |
803 (error "You have committed an error. | |
686 Try something else.") | 804 Try something else.") |
687 @error{} You have committed an error. | 805 @error{} You have committed an error. |
688 Try something else. | 806 Try something else. |
689 @end group | 807 @end group |
690 | 808 |
691 @group | 809 @group |
692 (error "You have committed %d errors." 10) | 810 (error "You have committed %d errors." 10) |
693 @error{} You have committed 10 errors. | 811 @error{} You have committed 10 errors. |
694 @end group | 812 @end group |
695 @end example | 813 @end example |
696 | |
697 @code{error} works by calling @code{signal} with two arguments: the | |
698 error symbol @code{error}, and a list containing the string returned by | |
699 @code{format}. This is repeated in an endless loop, to ensure that | |
700 @code{error} never returns. | |
701 | 814 |
702 If you want to use your own string as an error message verbatim, don't | 815 If you want to use your own string as an error message verbatim, don't |
703 just write @code{(error @var{string})}. If @var{string} contains | 816 just write @code{(error @var{string})}. If @var{string} contains |
704 @samp{%}, it will be interpreted as a format specifier, with undesirable | 817 @samp{%}, it will be interpreted as a format specifier, with undesirable |
705 results. Instead, use @code{(error "%s" @var{string})}. | 818 results. Instead, use @code{(error "%s" @var{string})}. |
706 @end defun | 819 @end defun |
707 | 820 |
708 @defun cerror format-string &rest args | 821 @defun cerror datum &rest args |
709 This function behaves like @code{error}, except that the error it | 822 This function behaves like @code{error}, except that the error it |
710 signals is continuable. That means that debugger commands @kbd{c} and | 823 signals is continuable. That means that debugger commands @kbd{c} and |
711 @kbd{r} can resume execution. | 824 @kbd{r} can resume execution. |
712 @end defun | 825 @end defun |
713 | 826 |
964 message and returns a very large number. | 1077 message and returns a very large number. |
965 | 1078 |
966 @smallexample | 1079 @smallexample |
967 @group | 1080 @group |
968 (defun safe-divide (dividend divisor) | 1081 (defun safe-divide (dividend divisor) |
969 (condition-case err | 1082 (condition-case err |
970 ;; @r{Protected form.} | 1083 ;; @r{Protected form.} |
971 (/ dividend divisor) | 1084 (/ dividend divisor) |
972 ;; @r{The handler.} | 1085 ;; @r{The handler.} |
973 (arith-error ; @r{Condition.} | 1086 (arith-error ; @r{Condition.} |
974 (princ (format "Arithmetic error: %s" err)) | 1087 (princ (format "Arithmetic error: %s" err)) |
975 1000000))) | 1088 1000000))) |
976 @result{} safe-divide | 1089 @result{} safe-divide |
1009 (if (eq baz 35) | 1122 (if (eq baz 35) |
1010 t | 1123 t |
1011 ;; @r{This is a call to the function @code{error}.} | 1124 ;; @r{This is a call to the function @code{error}.} |
1012 (error "Rats! The variable %s was %s, not 35" 'baz baz)) | 1125 (error "Rats! The variable %s was %s, not 35" 'baz baz)) |
1013 ;; @r{This is the handler; it is not a form.} | 1126 ;; @r{This is the handler; it is not a form.} |
1014 (error (princ (format "The error was: %s" err)) | 1127 (error (princ (format "The error was: %s" err)) |
1015 2)) | 1128 2)) |
1016 @print{} The error was: (error "Rats! The variable baz was 34, not 35") | 1129 @print{} The error was: (error "Rats! The variable baz was 34, not 35") |
1017 @result{} 2 | 1130 @result{} 2 |
1018 @end group | 1131 @end group |
1019 @end smallexample | 1132 @end smallexample |
1117 seriously decrease the power of @code{condition-case}. Condition names | 1230 seriously decrease the power of @code{condition-case}. Condition names |
1118 make it possible to categorize errors at various levels of generality | 1231 make it possible to categorize errors at various levels of generality |
1119 when you write an error handler. Using error symbols alone would | 1232 when you write an error handler. Using error symbols alone would |
1120 eliminate all but the narrowest level of classification. | 1233 eliminate all but the narrowest level of classification. |
1121 | 1234 |
1122 | 1235 |
1123 | 1236 |
1124 @xref{Standard Errors}, for a list of all the standard error symbols | 1237 @xref{Standard Errors}, for a list of all the standard error symbols |
1125 and their conditions. | 1238 and their conditions. |
1126 | 1239 |
1127 @node Cleanups | 1240 @node Cleanups |