comparison man/lispref/control.texi @ 412:697ef44129c6 r21-2-14

Import from CVS: tag r21-2-14
author cvs
date Mon, 13 Aug 2007 11:20:41 +0200
parents 74fd4e045ea6
children
comparison
equal deleted inserted replaced
411:12e008d41344 412:697ef44129c6
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}, @code{signal}, and others. 665 @code{error} and @code{signal}.
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
681 These examples show typical uses of @code{error}: 676 These examples show typical uses of @code{error}:
682 677
683 @example 678 @example
684 @group 679 @group
685 (error "You have committed an error. 680 (error "You have committed an error.
694 @end group 689 @end group
695 @end example 690 @end example
696 691
697 @code{error} works by calling @code{signal} with two arguments: the 692 @code{error} works by calling @code{signal} with two arguments: the
698 error symbol @code{error}, and a list containing the string returned by 693 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 694 @code{format}.
700 @code{error} never returns.
701 695
702 If you want to use your own string as an error message verbatim, don't 696 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 697 just write @code{(error @var{string})}. If @var{string} contains
704 @samp{%}, it will be interpreted as a format specifier, with undesirable 698 @samp{%}, it will be interpreted as a format specifier, with undesirable
705 results. Instead, use @code{(error "%s" @var{string})}. 699 results. Instead, use @code{(error "%s" @var{string})}.
706 @end defun 700 @end defun
707 701
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
714 @defun signal error-symbol data 702 @defun signal error-symbol data
715 This function signals a continuable error named by @var{error-symbol}. 703 This function signals an error named by @var{error-symbol}. The
716 The argument @var{data} is a list of additional Lisp objects relevant to 704 argument @var{data} is a list of additional Lisp objects relevant to the
717 the circumstances of the error. 705 circumstances of the error.
718 706
719 The argument @var{error-symbol} must be an @dfn{error symbol}---a symbol 707 The argument @var{error-symbol} must be an @dfn{error symbol}---a symbol
720 bearing a property @code{error-conditions} whose value is a list of 708 bearing a property @code{error-conditions} whose value is a list of
721 condition names. This is how XEmacs Lisp classifies different sorts of 709 condition names. This is how XEmacs Lisp classifies different sorts of
722 errors. 710 errors.
723 711
724 The number and significance of the objects in @var{data} depends on 712 The number and significance of the objects in @var{data} depends on
725 @var{error-symbol}. For example, with a @code{wrong-type-argument} 713 @var{error-symbol}. For example, with a @code{wrong-type-arg} error,
726 error, there are two objects in the list: a predicate that describes the 714 there are two objects in the list: a predicate that describes the type
727 type that was expected, and the object that failed to fit that type. 715 that was expected, and the object that failed to fit that type.
728 @xref{Error Symbols}, for a description of error symbols. 716 @xref{Error Symbols}, for a description of error symbols.
729 717
730 Both @var{error-symbol} and @var{data} are available to any error 718 Both @var{error-symbol} and @var{data} are available to any error
731 handlers that handle the error: @code{condition-case} binds a local 719 handlers that handle the error: @code{condition-case} binds a local
732 variable to a list of the form @code{(@var{error-symbol} .@: 720 variable to a list of the form @code{(@var{error-symbol} .@:
733 @var{data})} (@pxref{Handling Errors}). If the error is not handled, 721 @var{data})} (@pxref{Handling Errors}). If the error is not handled,
734 these two values are used in printing the error message. 722 these two values are used in printing the error message.
735 723
736 The function @code{signal} can return, if the debugger is invoked and 724 The function @code{signal} never returns (though in older Emacs versions
737 the user invokes the ``return from signal'' option. If you want the 725 it could sometimes return).
738 error not to be continuable, use @code{signal-error} instead. Note that
739 in FSF Emacs @code{signal} never returns.
740 726
741 @smallexample 727 @smallexample
742 @group 728 @group
743 (signal 'wrong-number-of-arguments '(x y)) 729 (signal 'wrong-number-of-arguments '(x y))
744 @error{} Wrong number of arguments: x, y 730 @error{} Wrong number of arguments: x, y
745 @end group 731 @end group
746 732
747 @group 733 @group
748 (signal 'no-such-error '("My unknown error condition")) 734 (signal 'no-such-error '("My unknown error condition."))
749 @error{} Peculiar error (no-such-error "My unknown error condition") 735 @error{} peculiar error: "My unknown error condition."
750 @end group 736 @end group
751 @end smallexample 737 @end smallexample
752 @end defun 738 @end defun
753 739
754 @defun signal-error error-symbol data 740 @cindex CL note---no continuable errors
755 This function behaves like @code{signal}, except that the error it 741 @quotation
756 signals is not continuable. 742 @b{Common Lisp note:} XEmacs Lisp has nothing like the Common Lisp
757 @end defun 743 concept of continuable errors.
758 744 @end quotation
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
784 745
785 @node Processing of Errors 746 @node Processing of Errors
786 @subsubsection How XEmacs Processes Errors 747 @subsubsection How XEmacs Processes Errors
787 748
788 When an error is signaled, @code{signal} searches for an active 749 When an error is signaled, @code{signal} searches for an active
797 If there is no applicable handler for the error, the current command is 758 If there is no applicable handler for the error, the current command is
798 terminated and control returns to the editor command loop, because the 759 terminated and control returns to the editor command loop, because the
799 command loop has an implicit handler for all kinds of errors. The 760 command loop has an implicit handler for all kinds of errors. The
800 command loop's handler uses the error symbol and associated data to 761 command loop's handler uses the error symbol and associated data to
801 print an error message. 762 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
823 763
824 @cindex @code{debug-on-error} use 764 @cindex @code{debug-on-error} use
825 An error that has no explicit handler may call the Lisp debugger. The 765 An error that has no explicit handler may call the Lisp debugger. The
826 debugger is enabled if the variable @code{debug-on-error} (@pxref{Error 766 debugger is enabled if the variable @code{debug-on-error} (@pxref{Error
827 Debugging}) is non-@code{nil}. Unlike error handlers, the debugger runs 767 Debugging}) is non-@code{nil}. Unlike error handlers, the debugger runs
892 predictable, such as failure to open a file in a call to 832 predictable, such as failure to open a file in a call to
893 @code{insert-file-contents}. It is also used to trap errors that are 833 @code{insert-file-contents}. It is also used to trap errors that are
894 totally unpredictable, such as when the program evaluates an expression 834 totally unpredictable, such as when the program evaluates an expression
895 read from the user. 835 read from the user.
896 836
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
904 Error signaling and handling have some resemblance to @code{throw} and 837 Error signaling and handling have some resemblance to @code{throw} and
905 @code{catch}, but they are entirely separate facilities. An error 838 @code{catch}, but they are entirely separate facilities. An error
906 cannot be caught by a @code{catch}, and a @code{throw} cannot be handled 839 cannot be caught by a @code{catch}, and a @code{throw} cannot be handled
907 by an error handler (though using @code{throw} when there is no suitable 840 by an error handler (though using @code{throw} when there is no suitable
908 @code{catch} signals an error that can be handled). 841 @code{catch} signals an error that can be handled).
982 @result{} 1000000 915 @result{} 1000000
983 @end group 916 @end group
984 @end smallexample 917 @end smallexample
985 918
986 @noindent 919 @noindent
987 The handler specifies condition name @code{arith-error} so that it will 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,
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,
990 921
991 @smallexample 922 @smallexample
992 @group 923 @group
993 (safe-divide nil 3) 924 (safe-divide nil 3)
994 @error{} Wrong type argument: integer-or-marker-p, nil 925 @error{} Wrong type argument: integer-or-marker-p, nil
1039 @code{error} which takes in all kinds of errors. Thus, each error has 970 @code{error} which takes in all kinds of errors. Thus, each error has
1040 one or more condition names: @code{error}, the error symbol if that 971 one or more condition names: @code{error}, the error symbol if that
1041 is distinct from @code{error}, and perhaps some intermediate 972 is distinct from @code{error}, and perhaps some intermediate
1042 classifications. 973 classifications.
1043 974
1044 In other words, each error condition @dfn{inherits} from another error 975 In order for a symbol to be an error symbol, it must have an
1045 condition, with @code{error} sitting at the top of the inheritance 976 @code{error-conditions} property which gives a list of condition names.
1046 hierarchy. 977 This list defines the conditions that this kind of error belongs to.
1047 978 (The error symbol itself, and the symbol @code{error}, should always be
1048 @defun define-error error-symbol error-message &optional inherits-from 979 members of this list.) Thus, the hierarchy of condition names is
1049 This function defines a new error, denoted by @var{error-symbol}. 980 defined by the @code{error-conditions} properties of the error symbols.
1050 @var{error-message} is an informative message explaining the error, and 981
1051 will be printed out when an unhandled error occurs. @var{error-symbol} 982 In addition to the @code{error-conditions} list, the error symbol
1052 is a sub-error of @var{inherits-from} (which defaults to @code{error}). 983 should have an @code{error-message} property whose value is a string to
1053 984 be printed when that error is signaled but not handled. If the
1054 @code{define-error} internally works by putting on @var{error-symbol} 985 @code{error-message} property exists, but is not a string, the error
1055 an @code{error-message} property whose value is @var{error-message}, and 986 message @samp{peculiar error} is used.
1056 an @code{error-conditions} property that is a list of @var{error-symbol} 987 @cindex peculiar error
1057 followed by each of its super-errors, up to and including @code{error}. 988
1058 You will sometimes see code that sets this up directly rather than 989 Here is how we define a new error symbol, @code{new-error}:
1059 calling @code{define-error}, but you should @emph{not} do this yourself, 990
1060 unless you wish to maintain compatibility with FSF Emacs, which does not 991 @example
1061 provide @code{define-error}. 992 @group
1062 @end defun 993 (put 'new-error
1063 994 'error-conditions
1064 Here is how we define a new error symbol, @code{new-error}, that 995 '(error my-own-errors new-error))
1065 belongs to a range of errors called @code{my-own-errors}: 996 @result{} (error my-own-errors new-error)
1066 997 @end group
1067 @example 998 @group
1068 @group 999 (put 'new-error 'error-message "A new error")
1069 (define-error 'my-own-errors "A whole range of errors" 'error) 1000 @result{} "A new error"
1070 (define-error 'new-error "A new error" 'my-own-errors) 1001 @end group
1071 @end group 1002 @end example
1072 @end example 1003
1073 1004 @noindent
1074 @noindent 1005 This error has three condition names: @code{new-error}, the narrowest
1075 @code{new-error} has three condition names: @code{new-error}, the 1006 classification; @code{my-own-errors}, which we imagine is a wider
1076 narrowest classification; @code{my-own-errors}, which we imagine is a 1007 classification; and @code{error}, which is the widest of all.
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.
1083 1008
1084 The error string should start with a capital letter but it should 1009 The error string should start with a capital letter but it should
1085 not end with a period. This is for consistency with the rest of Emacs. 1010 not end with a period. This is for consistency with the rest of Emacs.
1086 1011
1087 Naturally, XEmacs will never signal @code{new-error} on its own; only 1012 Naturally, XEmacs will never signal @code{new-error} on its own; only
1088 an explicit call to @code{signal} (@pxref{Signaling Errors}) in your 1013 an explicit call to @code{signal} (@pxref{Signaling Errors}) in your
1089 code can do this: 1014 code can do this:
1090 1015
1091 @example 1016 @example
1116 By contrast, using only error symbols without condition names would 1041 By contrast, using only error symbols without condition names would
1117 seriously decrease the power of @code{condition-case}. Condition names 1042 seriously decrease the power of @code{condition-case}. Condition names
1118 make it possible to categorize errors at various levels of generality 1043 make it possible to categorize errors at various levels of generality
1119 when you write an error handler. Using error symbols alone would 1044 when you write an error handler. Using error symbols alone would
1120 eliminate all but the narrowest level of classification. 1045 eliminate all but the narrowest level of classification.
1121
1122
1123 1046
1124 @xref{Standard Errors}, for a list of all the standard error symbols 1047 @xref{Standard Errors}, for a list of all the standard error symbols
1125 and their conditions. 1048 and their conditions.
1126 1049
1127 @node Cleanups 1050 @node Cleanups