comparison man/lispref/control.texi @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents 376386a54a3c
children 697ef44129c6 576fb035e263
comparison
equal deleted inserted replaced
397:f4aeb21a5bad 398:74fd4e045ea6
660 660
661 Most errors are signaled ``automatically'' within Lisp primitives 661 Most errors are signaled ``automatically'' within Lisp primitives
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} and @code{signal}. 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 @defun error format-string &rest args
672 This function signals an error with an error message constructed by 672 This function signals an error with an error message constructed by
673 applying @code{format} (@pxref{String Conversion}) to 673 applying @code{format} (@pxref{String Conversion}) to
674 @var{format-string} and @var{args}. 674 @var{format-string} and @var{args}.
675 675
676 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
678 user to be able to continue execution, use @code{cerror} or
679 @code{signal} instead.
680
676 These examples show typical uses of @code{error}: 681 These examples show typical uses of @code{error}:
677 682
678 @example 683 @example
679 @group 684 @group
680 (error "You have committed an error. 685 (error "You have committed an error.
689 @end group 694 @end group
690 @end example 695 @end example
691 696
692 @code{error} works by calling @code{signal} with two arguments: the 697 @code{error} works by calling @code{signal} with two arguments: the
693 error symbol @code{error}, and a list containing the string returned by 698 error symbol @code{error}, and a list containing the string returned by
694 @code{format}. 699 @code{format}. This is repeated in an endless loop, to ensure that
700 @code{error} never returns.
695 701
696 If you want to use your own string as an error message verbatim, don't 702 If you want to use your own string as an error message verbatim, don't
697 just write @code{(error @var{string})}. If @var{string} contains 703 just write @code{(error @var{string})}. If @var{string} contains
698 @samp{%}, it will be interpreted as a format specifier, with undesirable 704 @samp{%}, it will be interpreted as a format specifier, with undesirable
699 results. Instead, use @code{(error "%s" @var{string})}. 705 results. Instead, use @code{(error "%s" @var{string})}.
700 @end defun 706 @end defun
701 707
708 @defun cerror format-string &rest args
709 This function behaves like @code{error}, except that the error it
710 signals is continuable. That means that debugger commands @kbd{c} and
711 @kbd{r} can resume execution.
712 @end defun
713
702 @defun signal error-symbol data 714 @defun signal error-symbol data
703 This function signals an error named by @var{error-symbol}. The 715 This function signals a continuable error named by @var{error-symbol}.
704 argument @var{data} is a list of additional Lisp objects relevant to the 716 The argument @var{data} is a list of additional Lisp objects relevant to
705 circumstances of the error. 717 the circumstances of the error.
706 718
707 The argument @var{error-symbol} must be an @dfn{error symbol}---a symbol 719 The argument @var{error-symbol} must be an @dfn{error symbol}---a symbol
708 bearing a property @code{error-conditions} whose value is a list of 720 bearing a property @code{error-conditions} whose value is a list of
709 condition names. This is how XEmacs Lisp classifies different sorts of 721 condition names. This is how XEmacs Lisp classifies different sorts of
710 errors. 722 errors.
711 723
712 The number and significance of the objects in @var{data} depends on 724 The number and significance of the objects in @var{data} depends on
713 @var{error-symbol}. For example, with a @code{wrong-type-arg} error, 725 @var{error-symbol}. For example, with a @code{wrong-type-argument}
714 there are two objects in the list: a predicate that describes the type 726 error, there are two objects in the list: a predicate that describes the
715 that was expected, and the object that failed to fit that type. 727 type that was expected, and the object that failed to fit that type.
716 @xref{Error Symbols}, for a description of error symbols. 728 @xref{Error Symbols}, for a description of error symbols.
717 729
718 Both @var{error-symbol} and @var{data} are available to any error 730 Both @var{error-symbol} and @var{data} are available to any error
719 handlers that handle the error: @code{condition-case} binds a local 731 handlers that handle the error: @code{condition-case} binds a local
720 variable to a list of the form @code{(@var{error-symbol} .@: 732 variable to a list of the form @code{(@var{error-symbol} .@:
721 @var{data})} (@pxref{Handling Errors}). If the error is not handled, 733 @var{data})} (@pxref{Handling Errors}). If the error is not handled,
722 these two values are used in printing the error message. 734 these two values are used in printing the error message.
723 735
724 The function @code{signal} never returns (though in older Emacs versions 736 The function @code{signal} can return, if the debugger is invoked and
725 it could sometimes return). 737 the user invokes the ``return from signal'' option. If you want the
738 error not to be continuable, use @code{signal-error} instead. Note that
739 in FSF Emacs @code{signal} never returns.
726 740
727 @smallexample 741 @smallexample
728 @group 742 @group
729 (signal 'wrong-number-of-arguments '(x y)) 743 (signal 'wrong-number-of-arguments '(x y))
730 @error{} Wrong number of arguments: x, y 744 @error{} Wrong number of arguments: x, y
731 @end group 745 @end group
732 746
733 @group 747 @group
734 (signal 'no-such-error '("My unknown error condition.")) 748 (signal 'no-such-error '("My unknown error condition"))
735 @error{} peculiar error: "My unknown error condition." 749 @error{} Peculiar error (no-such-error "My unknown error condition")
736 @end group 750 @end group
737 @end smallexample 751 @end smallexample
738 @end defun 752 @end defun
739 753
740 @cindex CL note---no continuable errors 754 @defun signal-error error-symbol data
741 @quotation 755 This function behaves like @code{signal}, except that the error it
742 @b{Common Lisp note:} XEmacs Lisp has nothing like the Common Lisp 756 signals is not continuable.
743 concept of continuable errors. 757 @end defun
744 @end quotation 758
759 @defmac check-argument-type predicate argument
760 This macro checks that @var{argument} satisfies @var{predicate}. If
761 that is not the case, it signals a continuable
762 @code{wrong-type-argument} error until the returned value satisfies
763 @var{predicate}, and assigns the returned value to @var{argument}. In
764 other words, execution of the program will not continue until
765 @var{predicate} is met.
766
767 @var{argument} is not evaluated, and should be a symbol.
768 @var{predicate} is evaluated, and should name a function.
769
770 As shown in the following example, @code{check-argument-type} is useful
771 in low-level code that attempts to ensure the sanity of its data before
772 proceeding.
773
774 @example
775 @group
776 (defun cache-object-internal (object wlist)
777 ;; @r{Before doing anything, make sure that @var{wlist} is indeed}
778 ;; @r{a weak list, which is what we expect.}
779 (check-argument-type 'weak-list-p wlist)
780 @dots{})
781 @end group
782 @end example
783 @end defmac
745 784
746 @node Processing of Errors 785 @node Processing of Errors
747 @subsubsection How XEmacs Processes Errors 786 @subsubsection How XEmacs Processes Errors
748 787
749 When an error is signaled, @code{signal} searches for an active 788 When an error is signaled, @code{signal} searches for an active
758 If there is no applicable handler for the error, the current command is 797 If there is no applicable handler for the error, the current command is
759 terminated and control returns to the editor command loop, because the 798 terminated and control returns to the editor command loop, because the
760 command loop has an implicit handler for all kinds of errors. The 799 command loop has an implicit handler for all kinds of errors. The
761 command loop's handler uses the error symbol and associated data to 800 command loop's handler uses the error symbol and associated data to
762 print an error message. 801 print an error message.
802
803 Errors in command loop are processed using the @code{command-error}
804 function, which takes care of some necessary cleanup, and prints a
805 formatted error message to the echo area. The functions that do the
806 formatting are explained below.
807
808 @defun display-error error-object stream
809 This function displays @var{error-object} on @var{stream}.
810 @var{error-object} is a cons of error type, a symbol, and error
811 arguments, a list. If the error type symbol of one of its error
812 condition superclasses has an @code{display-error} property, that
813 function is invoked for printing the actual error message. Otherwise,
814 the error is printed as @samp{Error: arg1, arg2, ...}.
815 @end defun
816
817 @defun error-message-string error-object
818 This function converts @var{error-object} to an error message string,
819 and returns it. The message is equivalent to the one that would be
820 printed by @code{display-error}, except that it is conveniently returned
821 in string form.
822 @end defun
763 823
764 @cindex @code{debug-on-error} use 824 @cindex @code{debug-on-error} use
765 An error that has no explicit handler may call the Lisp debugger. The 825 An error that has no explicit handler may call the Lisp debugger. The
766 debugger is enabled if the variable @code{debug-on-error} (@pxref{Error 826 debugger is enabled if the variable @code{debug-on-error} (@pxref{Error
767 Debugging}) is non-@code{nil}. Unlike error handlers, the debugger runs 827 Debugging}) is non-@code{nil}. Unlike error handlers, the debugger runs
832 predictable, such as failure to open a file in a call to 892 predictable, such as failure to open a file in a call to
833 @code{insert-file-contents}. It is also used to trap errors that are 893 @code{insert-file-contents}. It is also used to trap errors that are
834 totally unpredictable, such as when the program evaluates an expression 894 totally unpredictable, such as when the program evaluates an expression
835 read from the user. 895 read from the user.
836 896
897 @cindex @code{debug-on-signal} use
898 Even when an error is handled, the debugger may still be called if the
899 variable @code{debug-on-signal} (@pxref{Error Debugging}) is
900 non-@code{nil}. Note that this may yield unpredictable results with
901 code that traps expected errors as normal part of its operation. Do not
902 set @code{debug-on-signal} unless you know what you are doing.
903
837 Error signaling and handling have some resemblance to @code{throw} and 904 Error signaling and handling have some resemblance to @code{throw} and
838 @code{catch}, but they are entirely separate facilities. An error 905 @code{catch}, but they are entirely separate facilities. An error
839 cannot be caught by a @code{catch}, and a @code{throw} cannot be handled 906 cannot be caught by a @code{catch}, and a @code{throw} cannot be handled
840 by an error handler (though using @code{throw} when there is no suitable 907 by an error handler (though using @code{throw} when there is no suitable
841 @code{catch} signals an error that can be handled). 908 @code{catch} signals an error that can be handled).
915 @result{} 1000000 982 @result{} 1000000
916 @end group 983 @end group
917 @end smallexample 984 @end smallexample
918 985
919 @noindent 986 @noindent
920 The handler specifies condition name @code{arith-error} so that it will handle only division-by-zero errors. Other kinds of errors will not be handled, at least not by this @code{condition-case}. Thus, 987 The handler specifies condition name @code{arith-error} so that it will
988 handle only division-by-zero errors. Other kinds of errors will not be
989 handled, at least not by this @code{condition-case}. Thus,
921 990
922 @smallexample 991 @smallexample
923 @group 992 @group
924 (safe-divide nil 3) 993 (safe-divide nil 3)
925 @error{} Wrong type argument: integer-or-marker-p, nil 994 @error{} Wrong type argument: integer-or-marker-p, nil
970 @code{error} which takes in all kinds of errors. Thus, each error has 1039 @code{error} which takes in all kinds of errors. Thus, each error has
971 one or more condition names: @code{error}, the error symbol if that 1040 one or more condition names: @code{error}, the error symbol if that
972 is distinct from @code{error}, and perhaps some intermediate 1041 is distinct from @code{error}, and perhaps some intermediate
973 classifications. 1042 classifications.
974 1043
975 In order for a symbol to be an error symbol, it must have an 1044 In other words, each error condition @dfn{inherits} from another error
976 @code{error-conditions} property which gives a list of condition names. 1045 condition, with @code{error} sitting at the top of the inheritance
977 This list defines the conditions that this kind of error belongs to. 1046 hierarchy.
978 (The error symbol itself, and the symbol @code{error}, should always be 1047
979 members of this list.) Thus, the hierarchy of condition names is 1048 @defun define-error error-symbol error-message &optional inherits-from
980 defined by the @code{error-conditions} properties of the error symbols. 1049 This function defines a new error, denoted by @var{error-symbol}.
981 1050 @var{error-message} is an informative message explaining the error, and
982 In addition to the @code{error-conditions} list, the error symbol 1051 will be printed out when an unhandled error occurs. @var{error-symbol}
983 should have an @code{error-message} property whose value is a string to 1052 is a sub-error of @var{inherits-from} (which defaults to @code{error}).
984 be printed when that error is signaled but not handled. If the 1053
985 @code{error-message} property exists, but is not a string, the error 1054 @code{define-error} internally works by putting on @var{error-symbol}
986 message @samp{peculiar error} is used. 1055 an @code{error-message} property whose value is @var{error-message}, and
987 @cindex peculiar error 1056 an @code{error-conditions} property that is a list of @var{error-symbol}
988 1057 followed by each of its super-errors, up to and including @code{error}.
989 Here is how we define a new error symbol, @code{new-error}: 1058 You will sometimes see code that sets this up directly rather than
990 1059 calling @code{define-error}, but you should @emph{not} do this yourself,
991 @example 1060 unless you wish to maintain compatibility with FSF Emacs, which does not
992 @group 1061 provide @code{define-error}.
993 (put 'new-error 1062 @end defun
994 'error-conditions 1063
995 '(error my-own-errors new-error)) 1064 Here is how we define a new error symbol, @code{new-error}, that
996 @result{} (error my-own-errors new-error) 1065 belongs to a range of errors called @code{my-own-errors}:
997 @end group 1066
998 @group 1067 @example
999 (put 'new-error 'error-message "A new error") 1068 @group
1000 @result{} "A new error" 1069 (define-error 'my-own-errors "A whole range of errors" 'error)
1001 @end group 1070 (define-error 'new-error "A new error" 'my-own-errors)
1002 @end example 1071 @end group
1003 1072 @end example
1004 @noindent 1073
1005 This error has three condition names: @code{new-error}, the narrowest 1074 @noindent
1006 classification; @code{my-own-errors}, which we imagine is a wider 1075 @code{new-error} has three condition names: @code{new-error}, the
1007 classification; and @code{error}, which is the widest of all. 1076 narrowest classification; @code{my-own-errors}, which we imagine is a
1077 wider classification; and @code{error}, which is the widest of all.
1078
1079 Note that it is not legal to try to define an error unless its
1080 super-error is also defined. For instance, attempting to define
1081 @code{new-error} before @code{my-own-errors} are defined will signal an
1082 error.
1008 1083
1009 The error string should start with a capital letter but it should 1084 The error string should start with a capital letter but it should
1010 not end with a period. This is for consistency with the rest of Emacs. 1085 not end with a period. This is for consistency with the rest of Emacs.
1011 1086
1012 Naturally, XEmacs will never signal @code{new-error} on its own; only 1087 Naturally, XEmacs will never signal @code{new-error} on its own; only
1013 an explicit call to @code{signal} (@pxref{Signaling Errors}) in your 1088 an explicit call to @code{signal} (@pxref{Signaling Errors}) in your
1014 code can do this: 1089 code can do this:
1015 1090
1016 @example 1091 @example
1041 By contrast, using only error symbols without condition names would 1116 By contrast, using only error symbols without condition names would
1042 seriously decrease the power of @code{condition-case}. Condition names 1117 seriously decrease the power of @code{condition-case}. Condition names
1043 make it possible to categorize errors at various levels of generality 1118 make it possible to categorize errors at various levels of generality
1044 when you write an error handler. Using error symbols alone would 1119 when you write an error handler. Using error symbols alone would
1045 eliminate all but the narrowest level of classification. 1120 eliminate all but the narrowest level of classification.
1121
1122
1046 1123
1047 @xref{Standard Errors}, for a list of all the standard error symbols 1124 @xref{Standard Errors}, for a list of all the standard error symbols
1048 and their conditions. 1125 and their conditions.
1049 1126
1050 @node Cleanups 1127 @node Cleanups