1
0
Fork 0
cl-sites/novaspec.org/cl/f_error.html
2025-02-05 18:52:26 +01:00

392 lines
No EOL
8.7 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>error | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_error.html"
><LINK REL="next" HREF="f_cerror.html" TYPE="text/html" TITLE="cerror"
><LINK REL="prev" HREF="f_assert.html" TYPE="text/html" TITLE="assert"
><LINK REL="up" HREF="9_2_Conditions_Dictionary.html" TYPE="text/html" TITLE="9.2 Conditions Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="9_Conditions.html"
>9. Conditions</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="9_2_Conditions_Dictionary.html"
>9.2 Conditions Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_error.html"
>error</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_assert.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_cerror.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="error"
></SPAN
><B
>error</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>error</B
> <SPAN CLASS="cmssi"
>datum</SPAN
> <SPAN CLASS="cmtt"
>&amp;rest</SPAN
> <SPAN CLASS="cmssi"
>arguments</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
>|</P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>datum</VAR
>, <VAR CLASS="param"
>arguments</VAR
> &#8212; <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designators</EM
></A
> for a <A HREF="26_1_Glossary.html#condition"
><EM CLASS="term"
>condition</EM
></A
> of default type <A HREF="t_simple-error.html" CLASS="typeref"
><B
>simple-error</B
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_error.html" CLASS="funref"
><B
>error</B
></A
> effectively invokes <A HREF="f_signal.html" CLASS="funref"
><B
>signal</B
></A
> on the denoted <A HREF="26_1_Glossary.html#condition"
><EM CLASS="term"
>condition</EM
></A
>. </P
><P CLASS="j"
>If the <A HREF="26_1_Glossary.html#condition"
><EM CLASS="term"
>condition</EM
></A
> is not handled, <CODE CLASS="f"
>(invoke-debugger <I CLASS="i"
><I
>condition</I
></I
>)</CODE
> is done. As a consequence of calling <A HREF="f_invoke-debugger.html" CLASS="funref"
><B
>invoke-debugger</B
></A
>, <A HREF="f_error.html" CLASS="funref"
><B
>error</B
></A
> cannot directly return; the only exit from <A HREF="f_error.html" CLASS="funref"
><B
>error</B
></A
> can come by non-local transfer of control in a handler or by use of an interactive debugging command.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (defun factorial (x)
(cond ((or (not (typep x 'integer)) (minusp x))
(error "~S is not a valid argument to FACTORIAL." x))
((zerop x) 1)
(t (* x (factorial (- x 1))))))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> FACTORIAL
(factorial 20)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2432902008176640000
(factorial -1)
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Error: -1 is not a valid argument to FACTORIAL.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> To continue, type :CONTINUE followed by an option number:
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1: Return to Lisp Toplevel.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Debug&gt;</PRE
><PRE CLASS="screen"
> (setq a 'fred)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> FRED
(if (numberp a) (1+ a) (error "~S is not a number." A))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Error: FRED is not a number.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> To continue, type :CONTINUE followed by an option number:
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1: Return to Lisp Toplevel.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Debug&gt; <U CLASS="underline"
><SPAN CLASS="cmtt"
>:Continue</SPAN
> <SPAN CLASS="cmtt"
>1</SPAN
></U
>
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Return to Lisp Toplevel.
(define-condition not-a-number (error)
((argument :reader not-a-number-argument :initarg :argument))
(:report (lambda (condition stream)
(format stream "~S is not a number."
(not-a-number-argument condition)))))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NOT-A-NUMBER
(if (numberp a) (1+ a) (error 'not-a-number :argument a))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Error: FRED is not a number.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> To continue, type :CONTINUE followed by an option number:
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1: Return to Lisp Toplevel.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Debug&gt; <U CLASS="underline"
><SPAN CLASS="cmtt"
>:Continue</SPAN
> <SPAN CLASS="cmtt"
>1</SPAN
></U
>
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Return to Lisp Toplevel.</PRE
></DD
><DT
><B
>Side Effects</B
></DT
><DD
><P CLASS="j"
><A HREF="26_1_Glossary.html#handler"
><EM CLASS="term"
>Handlers</EM
></A
> for the specified condition, if any, are invoked and might have side effects. Program execution might stop, and the debugger might be entered.</P
></DD
><DT
><B
>Affected By</B
></DT
><DD
><P CLASS="j"
>Existing handler bindings. </P
><P CLASS="j"
><A HREF="v_break-on-signals.html" CLASS="varref"
><B
>*break-on-signals*</B
></A
> </P
><P CLASS="j"
>Signals an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> if <VAR CLASS="param"
>datum</VAR
> and <VAR CLASS="param"
>arguments</VAR
> are not <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designators</EM
></A
> for a <A HREF="26_1_Glossary.html#condition"
><EM CLASS="term"
>condition</EM
></A
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_cerror.html" CLASS="funref"
><B
>cerror</B
></A
>, <A HREF="f_signal.html" CLASS="funref"
><B
>signal</B
></A
>, <A HREF="f_format.html" CLASS="funref"
><B
>format</B
></A
>, <A HREF="f_ignore-errors.html" CLASS="macref"
><B
>ignore-errors</B
></A
>, <A HREF="v_break-on-signals.html" CLASS="varref"
><B
>*break-on-signals*</B
></A
>, <A HREF="f_handler-bind.html" CLASS="macref"
><B
>handler-bind</B
></A
>, <A HREF="9_1_Condition_System_Concepts.html#sec_9_1" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>9.1</SPAN
> <SPAN CLASS="cmr"
>(Condition</SPAN
> <SPAN CLASS="cmr"
>System</SPAN
> <SPAN CLASS="cmr"
>Concepts)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>Some implementations may provide debugger commands for interactively returning from individual stack frames. However, it should be possible for the programmer to feel confident about writing code like:</P
><PRE CLASS="screen"
>(defun wargames:no-win-scenario ()
(if (error "pushing the button would be stupid."))
(push-the-button))</PRE
><P CLASS="j"
>In this scenario, there should be no chance that <A HREF="f_error.html" CLASS="funref"
><B
>error</B
></A
> will return and the button will get pushed. </P
><P CLASS="j"
>While the meaning of this program is clear and it might be proven &#8216;safe&#8217; by a formal theorem prover, such a proof is no guarantee that the program is safe to execute. Compilers have been known to have bugs, computers to have signal glitches, and human beings to manually intervene in ways that are not always possible to predict. Those kinds of errors, while beyond the scope of the condition system to formally model, are not beyond the scope of things that should seriously be considered when writing code that could have the kinds of sweeping effects hinted at by this example.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_assert.html" CLASS="prev"
>&#8592;</A
><A HREF="f_cerror.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>