Signaling an error means beginning error processing. Error processing normally aborts all or part of the running program and returns to a point that is set up to handle the error (see Processing of Errors). Here we describe how to signal an error.
Most errors are signaled “automatically” within Lisp primitives
which you call for other purposes, such as if you try to take the
car of an integer or move forward a character at the end of the
buffer. You can also signal errors explicitly with the functions
Quitting, which happens when the user types C-g, is not considered an error, but it is handled almost like an error. See Quitting.
Every error specifies an error message, one way or another. The message should state what is wrong (“File does not exist”), not how things ought to be (“File must exist”). The convention in Emacs Lisp is that error messages should start with a capital letter, but should not end with any sort of punctuation.
This function signals an error with an error message constructed by applying
format(see Formatting Strings) to format-string and args.
These examples show typical uses of
error:(error "That is an error -- try something else") error--> That is an error -- try something else (error "You have committed %d errors" 10) error--> You have committed 10 errors
errorworks by calling
signalwith two arguments: the error symbol
error, and a list containing the string returned by
Warning: If you want to use your own string as an error message verbatim, don't just write
). If string contains ‘%’, it will be interpreted as a format specifier, with undesirable results. Instead, use
The argument error-symbol must be an error symbol—a symbol bearing a property
error-conditionswhose value is a list of condition names. This is how Emacs Lisp classifies different sorts of errors. See Error Symbols, for a description of error symbols, error conditions and condition names.
If the error is not handled, the two arguments are used in printing the error message. Normally, this error message is provided by the
error-messageproperty of error-symbol. If data is non-
nil, this is followed by a colon and a comma separated list of the unevaluated elements of data. For
error, the error message is the car of data (that must be a string). Subcategories of
file-errorare handled specially.
The number and significance of the objects in data depends on error-symbol. For example, with a
wrong-type-argumenterror, there should be two objects in the list: a predicate that describes the type that was expected, and the object that failed to fit that type.
Both error-symbol and data are available to any error handlers that handle the error:
condition-casebinds a local variable to a list of the form
)(see Handling Errors).
signalnever returns.(signal 'wrong-number-of-arguments '(x y)) error--> Wrong number of arguments: x, y (signal 'no-such-error '("My unknown error condition")) error--> peculiar error: "My unknown error condition"
This function behaves exactly like
error, except that it uses the error symbol
error. As the name suggests, this is intended to report errors on the part of the user, rather than errors in the code itself. For example, if you try to use the command
Info-history-back(l) to move back beyond the start of your Info browsing history, Emacs signals a
user-error. Such errors do not cause entry to the debugger, even when
nil. See Error Debugging.
Common Lisp note: Emacs Lisp has nothing like the Common Lisp concept of continuable errors.