My various dotfiles

chap-9.texi 141KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273
  1. @node Conditions, Symbols, Structures, Top
  2. @chapter Conditions
  3. @menu
  4. * Condition System Concepts::
  5. * Conditions Dictionary::
  6. @end menu
  7. @node Condition System Concepts, Conditions Dictionary, Conditions, Conditions
  8. @section Condition System Concepts
  9. @c including concept-conditions
  10. Common Lisp constructs are described not only in terms of their
  11. behavior in situations during which they are intended to be used (see
  12. the ``Description'' part of each @i{operator} specification),
  13. but in all other situations (see the ``Exceptional Situations''
  14. part of each @i{operator} specification).
  15. A situation is the evaluation of an expression in a specific context.
  16. A @i{condition} is an @i{object} that
  17. represents a specific situation that has been detected.
  18. @i{Conditions} are @i{generalized instances} of the @i{class} @b{condition}.
  19. A hierarchy of @i{condition} classes is defined in @r{Common Lisp}.
  20. A @i{condition} has @i{slots} that contain data
  21. relevant to the situation that the @i{condition} represents.
  22. An error is a situation in which normal program execution cannot
  23. continue correctly without some form of intervention (either
  24. interactively by the user or under program control). Not all errors
  25. are detected. When an error goes undetected, the effects can be
  26. @i{implementation-dependent}, @i{implementation-defined}, unspecified, or
  27. undefined. See @ref{Definitions}. All detected errors can
  28. be represented by @i{conditions}, but not all
  29. @i{conditions} represent errors.
  30. Signaling is the process by which a @i{condition} can alter
  31. the flow of control in a program by raising the
  32. @i{condition} which can then be @i{handled}. The functions
  33. @b{error}, @b{cerror}, @b{signal}, and
  34. @b{warn} are used to signal @i{conditions}.
  35. The process of signaling involves the selection and invocation of a
  36. @i{handler} from a set of @i{active} @i{handlers}.
  37. A @i{handler} is a @i{function} of one argument (the
  38. @i{condition}) that is invoked to handle a @i{condition}.
  39. Each @i{handler} is associated with a @i{condition} @i{type},
  40. and a @i{handler} will be invoked only on a @i{condition} of the
  41. @i{handler}'s associated @i{type}.
  42. @i{Active} @i{handlers} are @i{established} dynamically
  43. (see @b{handler-bind} or @b{handler-case}).
  44. @i{Handlers} are invoked in a @i{dynamic environment}
  45. equivalent to that of the signaler,
  46. except that the set of @i{active} @i{handlers}
  47. is bound in such a way as to include only those that were @i{active}
  48. at the time the @i{handler} being invoked was @i{established}.
  49. Signaling a @i{condition} has no side-effect on the @i{condition},
  50. and there is no dynamic state contained in a @i{condition}.
  51. If a @i{handler} is invoked, it can address the @i{situation}
  52. in one of three ways:
  53. @table @asis
  54. @item @b{Decline}
  55. It can decline to @i{handle} the @i{condition}. It does this by
  56. simply returning rather than transferring control.
  57. When this happens, any values returned by the handler are
  58. ignored and the next most recently established handler is invoked.
  59. If there is no such handler and the signaling function is @b{error}
  60. or @b{cerror}, the debugger is entered in the
  61. @i{dynamic environment} of the signaler. If there is no such
  62. handler and the signaling function is either @b{signal} or
  63. @b{warn}, the signaling function simply returns~@b{nil}.
  64. @item @b{Handle}
  65. It can @i{handle} the @i{condition} by performing a non-local
  66. transfer of control. This can be done either primitively by using
  67. @b{go}, @b{return}, @b{throw} or more
  68. abstractly by using a function such as @b{abort} or
  69. @b{invoke-restart}.
  70. @item @b{Defer}
  71. It can put off a decision about whether to @i{handle} or @i{decline},
  72. by any of a number of actions, but most commonly by
  73. signaling another condition,
  74. resignaling the same condition,
  75. or forcing entry into the debugger.
  76. @end table
  77. @menu
  78. * Condition Types::
  79. * Creating Conditions::
  80. * Printing Conditions::
  81. * Signaling and Handling Conditions::
  82. * Assertions::
  83. * Notes about the Condition System's Background::
  84. @end menu
  85. @node Condition Types, Creating Conditions, Condition System Concepts, Condition System Concepts
  86. @subsection Condition Types
  87. Figure 9--1 lists the @i{standardized} @i{condition} @i{types}.
  88. Additional @i{condition} @i{types} can be defined by using @b{define-condition}.
  89. @group
  90. @noindent
  91. @w{ arithmetic-error floating-point-overflow simple-type-error }
  92. @w{ cell-error floating-point-underflow simple-warning }
  93. @w{ condition package-error storage-condition }
  94. @w{ control-error parse-error stream-error }
  95. @w{ division-by-zero print-not-readable style-warning }
  96. @w{ end-of-file program-error type-error }
  97. @w{ error reader-error unbound-slot }
  98. @w{ file-error serious-condition unbound-variable }
  99. @w{ floating-point-inexact simple-condition undefined-function }
  100. @w{ floating-point-invalid-operation simple-error warning }
  101. @noindent
  102. @w{ Figure 9--1: Standardized Condition Types }
  103. @end group
  104. All @i{condition} types are @i{subtypes} of @i{type} @b{condition}. That is,
  105. @example
  106. (typep @i{c} 'condition) @result{} @i{true}
  107. @end example
  108. if and only if @i{c} is a @i{condition}.
  109. @i{Implementations} must define all specified @i{subtype} relationships.
  110. Except where noted, all @i{subtype} relationships indicated in
  111. this document are not mutually exclusive.
  112. A @i{condition} inherits the structure of its @i{supertypes}.
  113. The metaclass of the @i{class} @b{condition} is not specified.
  114. @i{Names} of @i{condition} @i{types} may be used to specify
  115. @i{supertype} relationships in @b{define-condition},
  116. but the consequences are not specified if an attempt is made to use
  117. a @i{condition} @i{type} as a @i{superclass} in a @b{defclass} @i{form}.
  118. Figure 9--2 shows @i{operators} that
  119. define @i{condition} @i{types} and creating @i{conditions}.
  120. @group
  121. @noindent
  122. @w{ define-condition make-condition }
  123. @noindent
  124. @w{ Figure 9--2: Operators that define and create conditions.}
  125. @end group
  126. Figure 9--3 shows @i{operators} that @i{read}
  127. the @i{value} of @i{condition} @i{slots}.
  128. @group
  129. @noindent
  130. @w{ arithmetic-error-operands simple-condition-format-arguments }
  131. @w{ arithmetic-error-operation simple-condition-format-control }
  132. @w{ cell-error-name stream-error-stream }
  133. @w{ file-error-pathname type-error-datum }
  134. @w{ package-error-package type-error-expected-type }
  135. @w{ print-not-readable-object unbound-slot-instance }
  136. @noindent
  137. @w{ Figure 9--3: Operators that read condition slots. }
  138. @end group
  139. @menu
  140. * Serious Conditions::
  141. @end menu
  142. @node Serious Conditions, , Condition Types, Condition Types
  143. @subsubsection Serious Conditions
  144. A @i{serious condition} is a @i{condition} serious
  145. enough to require interactive intervention if not handled.
  146. @i{Serious conditions} are typically signaled with @b{error} or @b{cerror};
  147. non-serious @i{conditions} are typically signaled with @b{signal} or @b{warn}.
  148. @node Creating Conditions, Printing Conditions, Condition Types, Condition System Concepts
  149. @subsection Creating Conditions
  150. The function @b{make-condition} can be used to construct
  151. a @i{condition} @i{object} explicitly. Functions such as @b{error},
  152. @b{cerror}, @b{signal}, and @b{warn} operate on
  153. @i{conditions} and might create @i{condition} @i{objects}
  154. implicitly. Macros such as @b{ccase}, @b{ctypecase},
  155. @b{ecase}, @b{etypecase}, @b{check-type}, and
  156. @b{assert} might also implicitly create (and @i{signal})
  157. @i{conditions}.
  158. @menu
  159. * Condition Designators::
  160. @end menu
  161. @node Condition Designators, , Creating Conditions, Creating Conditions
  162. @subsubsection Condition Designators
  163. A number of the functions in the condition system take arguments which
  164. are identified as @i{condition designators}
  165. @IGindex{condition designator}
  166. .
  167. By convention, those arguments are notated as
  168. @i{datum} {&rest} @i{arguments}
  169. Taken together, the @i{datum} and the @i{arguments} are
  170. ``@i{designators} for a @i{condition} of default type @i{default-type}.''
  171. How the denoted @i{condition} is computed depends on the type of the @i{datum}:
  172. @table @asis
  173. @item {@t{*}} If the @i{datum} is a @i{symbol}
  174. naming a @i{condition} @i{type} ...
  175. The denoted @i{condition} is the result of
  176. @example
  177. (apply #'make-condition @i{datum} @i{arguments})
  178. @end example
  179. @item {@t{*}} If the @i{datum} is a @i{format control} ...
  180. The denoted @i{condition} is the result of
  181. @example
  182. (make-condition @i{defaulted-type}
  183. :format-control @i{datum}
  184. :format-arguments @i{arguments})
  185. @end example
  186. where the @i{defaulted-type} is a @i{subtype} of @i{default-type}.
  187. @item {@t{*}} If the @i{datum} is a @i{condition} ...
  188. The denoted @i{condition} is the @i{datum} itself.
  189. In this case, unless otherwise specified by the description of the
  190. @i{operator} in question, the @i{arguments} must be @i{null};
  191. that is, the consequences are undefined if any @i{arguments} were supplied.
  192. @end table
  193. Note that the @i{default-type} gets used only in the case where
  194. the @i{datum} @i{string} is supplied. In the other situations,
  195. the resulting condition is not necessarily of @i{type} @i{default-type}.
  196. Here are some illustrations of how different @i{condition designators}
  197. can denote equivalent @i{condition} @i{objects}:
  198. @example
  199. (let ((c (make-condition 'arithmetic-error :operator '/ :operands '(7 0))))
  200. (error c))
  201. @equiv{} (error 'arithmetic-error :operator '/ :operands '(7 0))
  202. (error "Bad luck.")
  203. @equiv{} (error 'simple-error :format-control "Bad luck." :format-arguments '())
  204. @end example
  205. @node Printing Conditions, Signaling and Handling Conditions, Creating Conditions, Condition System Concepts
  206. @subsection Printing Conditions
  207. If the @t{:report} argument to @b{define-condition} is used,
  208. a print function is defined that is called whenever
  209. the defined @i{condition} is printed while the @i{value} of @b{*print-escape*} is @i{false}.
  210. This function is called the @i{condition reporter}
  211. @IGindex{condition reporter}
  212. ;
  213. the text which it outputs is called a @i{report message}
  214. @IGindex{report message}
  215. .
  216. When a @i{condition} is printed and @b{*print-escape*}
  217. is @i{false}, the @i{condition reporter} for the @i{condition} is invoked.
  218. @i{Conditions} are printed automatically by functions such as
  219. @b{invoke-debugger}, @b{break}, and @b{warn}.
  220. When @b{*print-escape*} is @i{true}, the @i{object} should print in an
  221. abbreviated fashion according to the style of the implementation
  222. (@i{e.g.}, by @b{print-unreadable-object}). It is not required that a
  223. @i{condition} can be recreated by reading its printed representation.
  224. No @i{function} is provided for directly @i{accessing}
  225. or invoking @i{condition reporters}.
  226. @menu
  227. * Recommended Style in Condition Reporting::
  228. * Capitalization and Punctuation in Condition Reports::
  229. * Leading and Trailing Newlines in Condition Reports::
  230. * Embedded Newlines in Condition Reports::
  231. * Note about Tabs in Condition Reports::
  232. * Mentioning Containing Function in Condition Reports::
  233. @end menu
  234. @node Recommended Style in Condition Reporting, Capitalization and Punctuation in Condition Reports, Printing Conditions, Printing Conditions
  235. @subsubsection Recommended Style in Condition Reporting
  236. In order to ensure a properly aesthetic result when presenting
  237. @i{report messages} to the user, certain stylistic conventions are
  238. recommended.
  239. There are stylistic recommendations for the content of the messages
  240. output by @i{condition reporters}, but there are no formal requirements
  241. on those @i{programs}.
  242. If a @i{program} violates the recommendations for some message, the
  243. display of that message might be less aesthetic than if the guideline
  244. had been observed, but the @i{program} is still considered a
  245. @i{conforming program}.
  246. The requirements on a @i{program} or @i{implementation} which
  247. invokes a @i{condition reporter} are somewhat stronger. A @i{conforming
  248. program} must be permitted to assume that if these style guidelines are
  249. followed, proper aesthetics will be maintained. Where appropriate, any
  250. specific requirements on such routines are explicitly mentioned below.
  251. @node Capitalization and Punctuation in Condition Reports, Leading and Trailing Newlines in Condition Reports, Recommended Style in Condition Reporting, Printing Conditions
  252. @subsubsection Capitalization and Punctuation in Condition Reports
  253. It is recommended that a @i{report message} be a complete sentences, in the
  254. proper case and correctly punctuated. In English, for example, this
  255. means the first letter should be uppercase, and there should be a
  256. trailing period.
  257. @example
  258. (error "This is a message") ; Not recommended
  259. (error "this is a message.") ; Not recommended
  260. (error "This is a message.") ; Recommended instead
  261. @end example
  262. @node Leading and Trailing Newlines in Condition Reports, Embedded Newlines in Condition Reports, Capitalization and Punctuation in Condition Reports, Printing Conditions
  263. @subsubsection Leading and Trailing Newlines in Condition Reports
  264. It is recommended that a @i{report message} not begin with any
  265. introductory text, such as ``@t{Error: }'' or ``@t{Warning: }''
  266. or even just @i{freshline} or @i{newline}.
  267. Such text is added, if appropriate to the context,
  268. by the routine invoking the @i{condition reporter}.
  269. It is recommended that a @i{report message} not be followed
  270. by a trailing @i{freshline} or @i{newline}.
  271. Such text is added, if appropriate to the context,
  272. by the routine invoking the @i{condition reporter}.
  273. @example
  274. (error "This is a message.~
  275. (error "~&This is a message.") ; Not recommended
  276. (error "~&This is a message.~
  277. (error "This is a message.") ; Recommended instead
  278. @end example
  279. @node Embedded Newlines in Condition Reports, Note about Tabs in Condition Reports, Leading and Trailing Newlines in Condition Reports, Printing Conditions
  280. @subsubsection Embedded Newlines in Condition Reports
  281. Especially if it is long, it is permissible and appropriate for
  282. a @i{report message} to contain one or more embedded @i{newlines}.
  283. If the calling routine conventionally inserts some additional prefix
  284. (such as ``@t{Error: }'' or ``@t{;; Error: }'') on the first line of
  285. the message, it must also assure that an appropriate prefix will be
  286. added to each subsequent line of the output, so that the left edge of
  287. the message output by the @i{condition reporter} will still be properly
  288. aligned.
  289. @example
  290. (defun test ()
  291. (error "This is an error message.~%It has two lines."))
  292. ;; Implementation A
  293. (test)
  294. This is an error message.
  295. It has two lines.
  296. ;; Implementation B
  297. (test)
  298. ;; Error: This is an error message.
  299. ;; It has two lines.
  300. ;; Implementation C
  301. (test)
  302. >> Error: This is an error message.
  303. It has two lines.
  304. @end example
  305. @node Note about Tabs in Condition Reports, Mentioning Containing Function in Condition Reports, Embedded Newlines in Condition Reports, Printing Conditions
  306. @subsubsection Note about Tabs in Condition Reports
  307. Because the indentation of a @i{report message} might be shifted to the right or
  308. left by an arbitrary amount, special care should be taken with the
  309. semi-standard @i{character} <@i{Tab}>
  310. (in those @i{implementations} that support such a @i{character}).
  311. Unless the @i{implementation} specifically defines its behavior
  312. in this context, its use should be avoided.
  313. @node Mentioning Containing Function in Condition Reports, , Note about Tabs in Condition Reports, Printing Conditions
  314. @subsubsection Mentioning Containing Function in Condition Reports
  315. The name of the containing function should generally not be mentioned in
  316. @i{report messages}. It is assumed that the @i{debugger} will make this
  317. information accessible in situations where it is necessary and appropriate.
  318. @node Signaling and Handling Conditions, Assertions, Printing Conditions, Condition System Concepts
  319. @subsection Signaling and Handling Conditions
  320. The operation of the condition system depends on the ordering of
  321. active @i{applicable handlers} from most recent to least recent.
  322. Each @i{handler} is associated with a @i{type specifier}
  323. that must designate a @i{subtype} of @i{type} @b{condition}. A @i{handler}
  324. is said to be @i{applicable} to a @i{condition} if that
  325. @i{condition} is of the @i{type} designated by the associated
  326. @i{type specifier}.
  327. @i{Active} @i{handlers} are @i{established} by using
  328. @b{handler-bind} (or an abstraction based on @b{handler-bind},
  329. such as @b{handler-case} or @b{ignore-errors}).
  330. @i{Active} @i{handlers} can be @i{established} within the
  331. dynamic scope of other @i{active} @i{handlers}.
  332. At any point during program execution, there is a set of @i{active} @i{handlers}.
  333. When a @i{condition} is signaled, the @i{most recent} active @i{applicable handler}
  334. for that @i{condition} is selected from this set.
  335. Given a @i{condition}, the order of recentness of
  336. active @i{applicable handlers} is defined by the following two rules:
  337. @table @asis
  338. @item 1.
  339. Each handler in a set of active handlers H_1 is
  340. more recent than every handler in a set H_2 if the
  341. handlers in H_2 were active when the handlers in H_1 were
  342. established.
  343. @item 2.
  344. Let h_1 and h_2 be two applicable active
  345. handlers established by the same @i{form}. Then h_1 is
  346. more recent than h_2 if h_1 was defined to the left of
  347. h_2 in the @i{form} that established them.
  348. @end table
  349. Once a handler in a handler binding @i{form} (such as
  350. @b{handler-bind} or @b{handler-case}) has been selected, all
  351. handlers in that @i{form} become inactive for
  352. the remainder of the signaling process.
  353. While the selected @i{handler} runs, no other @i{handler} established
  354. by that @i{form} is active. That is, if the @i{handler} declines,
  355. no other handler established by that @i{form} will be considered for possible invocation.
  356. Figure 9--4 shows @i{operators} relating to
  357. the @i{handling} of @i{conditions}.
  358. @group
  359. @noindent
  360. @w{ handler-bind handler-case ignore-errors }
  361. @noindent
  362. @w{ Figure 9--4: Operators relating to handling conditions.}
  363. @end group
  364. @menu
  365. * Signaling::
  366. * Resignaling a Condition::
  367. * Restarts::
  368. * Interactive Use of Restarts::
  369. * Interfaces to Restarts::
  370. * Restart Tests::
  371. * Associating a Restart with a Condition::
  372. @end menu
  373. @node Signaling, Resignaling a Condition, Signaling and Handling Conditions, Signaling and Handling Conditions
  374. @subsubsection Signaling
  375. When a @i{condition} is signaled, the most recent
  376. applicable @i{active} @i{handler} is invoked.
  377. Sometimes a handler will decline by simply returning
  378. without a transfer of control.
  379. In such cases, the next most recent applicable active handler is
  380. invoked.
  381. If there are no applicable handlers for a @i{condition} that
  382. has been signaled, or if all applicable handlers decline, the
  383. @i{condition} is unhandled.
  384. The functions @b{cerror} and @b{error} invoke the
  385. interactive @i{condition} handler (the debugger) rather than
  386. return if the @i{condition} being signaled, regardless of
  387. its @i{type}, is unhandled. In contrast, @b{signal}
  388. returns @b{nil} if the @i{condition} being signaled,
  389. regardless of its @i{type}, is unhandled.
  390. The @i{variable} @b{*break-on-signals*} can be used to cause the
  391. debugger to be entered before the signaling process begins.
  392. Figure 9--5 shows @i{defined names} relating to
  393. the @i{signaling} of @i{conditions}.
  394. @group
  395. @noindent
  396. @w{ *break-on-signals* error warn }
  397. @w{ cerror signal }
  398. @noindent
  399. @w{ Figure 9--5: Defined names relating to signaling conditions.}
  400. @end group
  401. @node Resignaling a Condition, Restarts, Signaling, Signaling and Handling Conditions
  402. @subsubsection Resignaling a Condition
  403. During the @i{dynamic extent} of the @i{signaling} process for
  404. a particular @i{condition} @i{object},
  405. @b{signaling} the same @i{condition} @i{object} again
  406. is permitted if and only if the @i{situation} represented in both
  407. cases are the same.
  408. For example, a @i{handler} might legitimately @i{signal}
  409. the @i{condition} @i{object} that is its @i{argument}
  410. in order to allow outer @i{handlers} first opportunity to @i{handle}
  411. the condition. (Such a @i{handlers} is sometimes called a ``default handler.'')
  412. This action is permitted because the @i{situation} which the second
  413. @i{signaling} process is addressing is really the same @i{situation}.
  414. On the other hand, in an @i{implementation} that implemented asynchronous
  415. keyboard events by interrupting the user process with a call to @b{signal},
  416. it would not be permissible for two distinct asynchronous keyboard events
  417. to @i{signal} @i{identical} @i{condition} @i{objects}
  418. at the same time for different
  419. situations.
  420. @node Restarts, Interactive Use of Restarts, Resignaling a Condition, Signaling and Handling Conditions
  421. @subsubsection Restarts
  422. The interactive condition handler returns only through
  423. non-local transfer of control to specially defined @i{restarts}
  424. that can be set up either by the system or by user code. Transferring
  425. control to a restart is called ``invoking'' the restart. Like
  426. handlers, active @i{restarts} are @i{established}
  427. dynamically, and
  428. only active @i{restarts}
  429. can be invoked. An active
  430. @i{restart} can be invoked by the user from
  431. the debugger or by a program by using @b{invoke-restart}.
  432. A @i{restart} contains a
  433. @i{function} to be @i{called} when the @i{restart} is
  434. invoked, an optional name that can be used to find or invoke the
  435. @i{restart}, and
  436. an optional set of interaction information for the debugger to use to
  437. enable the user to manually invoke a @i{restart}.
  438. The name of a @i{restart} is
  439. used by @b{invoke-restart}. @i{Restarts} that can be invoked
  440. only within the debugger do not need names.
  441. @i{Restarts} can be established by using @b{restart-bind},
  442. @b{restart-case}, and @b{with-simple-restart}.
  443. A @i{restart} function can itself invoke any other @i{restart}
  444. that was active at the time of establishment of the @i{restart}
  445. of which the @i{function} is part.
  446. The @i{restarts} @i{established} by
  447. a @b{restart-bind} @i{form},
  448. a @b{restart-case} @i{form},
  449. or a @b{with-simple-restart} @i{form}
  450. have @i{dynamic extent}
  451. which extends for the duration of that @i{form}'s execution.
  452. @i{Restarts} of the same name can be ordered from least recent to
  453. most recent according to the following two rules:
  454. @table @asis
  455. @item 1.
  456. Each @i{restart} in a set of active restarts
  457. R_1 is more recent than every @i{restart} in a
  458. set R_2 if the @i{restarts}
  459. in R_2 were active when the @i{restarts} in R_1 were
  460. established.
  461. @item 2.
  462. Let r_1 and r_2 be two active @i{restarts} with
  463. the same name established by the same @i{form}. Then r_1 is
  464. more recent than r_2 if r_1 was defined to the
  465. left of r_2 in the @i{form} that established them.
  466. @end table
  467. If a @i{restart} is invoked but does not transfer control,
  468. the values resulting from the @i{restart} function are
  469. returned by the function that invoked the restart, either
  470. @b{invoke-restart} or @b{invoke-restart-interactively}.
  471. @node Interactive Use of Restarts, Interfaces to Restarts, Restarts, Signaling and Handling Conditions
  472. @subsubsection Interactive Use of Restarts
  473. For interactive handling, two pieces of information are needed
  474. from a @i{restart}: a report function and an interactive function.
  475. The report function
  476. is used by a program such as the debugger to
  477. present a description of the action the @i{restart} will take.
  478. The report function is specified and established by the
  479. @t{:report-function} keyword to
  480. @b{restart-bind} or the
  481. @t{:report} keyword to @b{restart-case}.
  482. The interactive function, which can be specified using the
  483. @t{:interactive-function} keyword to
  484. @b{restart-bind} or @t{:interactive} keyword
  485. to @b{restart-case}, is used when the @i{restart}
  486. is invoked
  487. interactively, such as from the debugger, to produce a suitable
  488. list of arguments.
  489. @b{invoke-restart} invokes the most recently @i{established}
  490. @i{restart} whose
  491. name is the same as the first argument to @b{invoke-restart}.
  492. If a @i{restart} is invoked interactively by the debugger and does
  493. not transfer control but rather returns values, the precise
  494. action of the debugger on those values is @i{implementation-defined}.
  495. @node Interfaces to Restarts, Restart Tests, Interactive Use of Restarts, Signaling and Handling Conditions
  496. @subsubsection Interfaces to Restarts
  497. Some @i{restarts} have functional interfaces,
  498. such as @b{abort}, @b{continue},
  499. @b{muffle-warning}, @b{store-value}, and
  500. @b{use-value}.
  501. They are ordinary functions that use
  502. @b{find-restart} and @b{invoke-restart} internally,
  503. that have the same name as the @i{restarts} they manipulate,
  504. and that are provided simply for notational convenience.
  505. Figure 9--6 shows @i{defined names} relating to
  506. @i{restarts}.
  507. @group
  508. @noindent
  509. @w{ abort invoke-restart-interactively store-value }
  510. @w{ compute-restarts muffle-warning use-value }
  511. @w{ continue restart-bind with-simple-restart }
  512. @w{ find-restart restart-case }
  513. @w{ invoke-restart restart-name }
  514. @noindent
  515. @w{ Figure 9--6: Defined names relating to restarts. }
  516. @end group
  517. @node Restart Tests, Associating a Restart with a Condition, Interfaces to Restarts, Signaling and Handling Conditions
  518. @subsubsection Restart Tests
  519. Each @i{restart} has an associated test, which is a function of one
  520. argument (a @i{condition} or @b{nil}) which returns @i{true} if the @i{restart}
  521. should be visible in the current @i{situation}. This test is created by
  522. the @t{:test-function} option to @b{restart-bind} or
  523. the @t{:test} option to @b{restart-case}.
  524. @node Associating a Restart with a Condition, , Restart Tests, Signaling and Handling Conditions
  525. @subsubsection Associating a Restart with a Condition
  526. A @i{restart} can be ``associated with'' a @i{condition} explicitly
  527. by @b{with-condition-restarts}, or implicitly by @b{restart-case}.
  528. Such an assocation has @i{dynamic extent}.
  529. A single @i{restart} may be associated with several @i{conditions}
  530. at the same time.
  531. A single @i{condition} may have several associated @i{restarts}
  532. at the same time.
  533. Active restarts associated with a particular @i{condition} can be detected
  534. by @i{calling} a @i{function} such as @b{find-restart}, supplying
  535. that @i{condition} as the @i{condition} @i{argument}.
  536. Active restarts can also be detected without regard to any associated
  537. @i{condition} by calling such a function without a @i{condition} @i{argument},
  538. or by supplying a value of @b{nil} for such an @i{argument}.
  539. @node Assertions, Notes about the Condition System's Background, Signaling and Handling Conditions, Condition System Concepts
  540. @subsection Assertions
  541. Conditional signaling of @i{conditions}
  542. based on such things as key match, form evaluation,
  543. and @i{type} are handled by assertion @i{operators}.
  544. Figure 9--7 shows @i{operators} relating to assertions.
  545. @group
  546. @noindent
  547. @w{ assert check-type ecase }
  548. @w{ ccase ctypecase etypecase }
  549. @noindent
  550. @w{ Figure 9--7: Operators relating to assertions.}
  551. @end group
  552. @node Notes about the Condition System's Background, , Assertions, Condition System Concepts
  553. @subsection Notes about the Condition System's Background
  554. For a background reference to the abstract concepts detailed in this
  555. section, see @i{Exceptional Situations in Lisp}. The details of that paper are not binding on
  556. this document, but may be helpful in establishing a conceptual basis for
  557. understanding this material.
  558. @c end of including concept-conditions
  559. @node Conditions Dictionary, , Condition System Concepts, Conditions
  560. @section Conditions Dictionary
  561. @c including dict-conditions
  562. @menu
  563. * condition::
  564. * warning::
  565. * style-warning::
  566. * serious-condition::
  567. * error (Condition Type)::
  568. * cell-error::
  569. * cell-error-name::
  570. * parse-error::
  571. * storage-condition::
  572. * assert::
  573. * error::
  574. * cerror::
  575. * check-type::
  576. * simple-error::
  577. * invalid-method-error::
  578. * method-combination-error::
  579. * signal::
  580. * simple-condition::
  581. * simple-condition-format-control::
  582. * warn::
  583. * simple-warning::
  584. * invoke-debugger::
  585. * break::
  586. * *debugger-hook*::
  587. * *break-on-signals*::
  588. * handler-bind::
  589. * handler-case::
  590. * ignore-errors::
  591. * define-condition::
  592. * make-condition::
  593. * restart::
  594. * compute-restarts::
  595. * find-restart::
  596. * invoke-restart::
  597. * invoke-restart-interactively::
  598. * restart-bind::
  599. * restart-case::
  600. * restart-name::
  601. * with-condition-restarts::
  602. * with-simple-restart::
  603. * abort::
  604. * continue::
  605. * muffle-warning::
  606. * store-value::
  607. * use-value::
  608. * abort::
  609. @end menu
  610. @node condition, warning, Conditions Dictionary, Conditions Dictionary
  611. @subsection condition [Condition Type]
  612. [Reviewer Note by Barrett: I think CONDITION-RESTARTS is not fully integrated.]
  613. @subsubheading Class Precedence List::
  614. @b{condition},
  615. @b{t}
  616. @subsubheading Description::
  617. All types of @i{conditions}, whether error or
  618. non-error, must inherit from this @i{type}.
  619. No additional @i{subtype} relationships among the specified @i{subtypes} of @i{type} @b{condition}
  620. are allowed, except when explicitly mentioned in the text; however
  621. implementations are permitted to introduce additional @i{types}
  622. and one of these @i{types} can be a @i{subtype} of any
  623. number of the @i{subtypes} of @i{type} @b{condition}.
  624. Whether a user-defined @i{condition} @i{type} has @i{slots}
  625. that are accessible by @i{with-slots} is @i{implementation-dependent}.
  626. Furthermore, even in an @i{implementation}
  627. in which user-defined @i{condition} @i{types} would have @i{slots},
  628. it is @i{implementation-dependent} whether any @i{condition}
  629. @i{types} defined in this document have such @i{slots} or,
  630. if they do, what their @i{names} might be;
  631. only the reader functions documented by this specification may be relied
  632. upon by portable code.
  633. @i{Conforming code} must observe the following restrictions related to
  634. @i{conditions}:
  635. @table @asis
  636. @item @t{*}
  637. @b{define-condition}, not @b{defclass}, must be used
  638. to define new @i{condition} @i{types}.
  639. @item @t{*}
  640. @b{make-condition}, not @b{make-instance}, must be used to
  641. create @i{condition} @i{objects} explicitly.
  642. @item @t{*}
  643. The @t{:report} option of @b{define-condition}, not @b{defmethod}
  644. for @b{print-object}, must be used to define a condition reporter.
  645. @item @t{*}
  646. @b{slot-value}, @b{slot-boundp}, @b{slot-makunbound},
  647. and @b{with-slots} must not be used on @i{condition} @i{objects}.
  648. Instead, the appropriate accessor functions (defined by @b{define-condition})
  649. should be used.
  650. @end table
  651. @node warning, style-warning, condition, Conditions Dictionary
  652. @subsection warning [Condition Type]
  653. @subsubheading Class Precedence List::
  654. @b{warning},
  655. @b{condition},
  656. @b{t}
  657. @subsubheading Description::
  658. The @i{type} @b{warning} consists of all types of warnings.
  659. @subsubheading See Also::
  660. @b{style-warning}
  661. @node style-warning, serious-condition, warning, Conditions Dictionary
  662. @subsection style-warning [Condition Type]
  663. @subsubheading Class Precedence List::
  664. @b{style-warning},
  665. @b{warning},
  666. @b{condition},
  667. @b{t}
  668. @subsubheading Description::
  669. The @i{type} @b{style-warning} includes those @i{conditions}
  670. that represent @i{situations} involving @i{code}
  671. that is @i{conforming code} but that is nevertheless
  672. considered to be faulty or substandard.
  673. @subsubheading See Also::
  674. @ref{muffle-warning}
  675. @subsubheading Notes::
  676. An @i{implementation} might signal such a @i{condition}
  677. if it encounters @i{code}
  678. that uses deprecated features
  679. or that appears unaesthetic or inefficient.
  680. An `unused variable' warning must be of @i{type} @b{style-warning}.
  681. In general, the question of whether @i{code} is faulty or substandard
  682. is a subjective decision to be made by the facility processing that @i{code}.
  683. The intent is that whenever such a facility wishes to complain about
  684. @i{code} on such subjective grounds, it should use this
  685. @i{condition} @i{type} so that any clients who wish to redirect or
  686. muffle superfluous warnings can do so without risking that they will be
  687. redirecting or muffling other, more serious warnings.
  688. @node serious-condition, error (Condition Type), style-warning, Conditions Dictionary
  689. @subsection serious-condition [Condition Type]
  690. @subsubheading Class Precedence List::
  691. @b{serious-condition},
  692. @b{condition},
  693. @b{t}
  694. @subsubheading Description::
  695. All @i{conditions} serious enough to require interactive intervention
  696. if not handled should inherit from the @i{type} @b{serious-condition}.
  697. This condition type is provided
  698. primarily so that it may be included as
  699. a @i{superclass} of other @i{condition} @i{types};
  700. it is not intended to be signaled directly.
  701. @subsubheading Notes::
  702. Signaling a @i{serious condition} does not itself force entry into
  703. the debugger. However, except in the unusual situation where the
  704. programmer can assure that no harm will come from failing to
  705. @i{handle} a @i{serious condition}, such a @i{condition} is
  706. usually signaled with @b{error} rather than @b{signal} in
  707. order to assure that the program does not continue without
  708. @i{handling} the @i{condition}. (And conversely, it is
  709. conventional to use @b{signal} rather than @b{error} to signal
  710. conditions which are not @i{serious conditions}, since normally the
  711. failure to handle a non-serious condition is not reason enough for the
  712. debugger to be entered.)
  713. @node error (Condition Type), cell-error, serious-condition, Conditions Dictionary
  714. @subsection error [Condition Type]
  715. @subsubheading Class Precedence List::
  716. @b{error},
  717. @b{serious-condition},
  718. @b{condition},
  719. @b{t}
  720. @subsubheading Description::
  721. The @i{type} @b{error} consists of all @i{conditions} that represent @i{errors}.
  722. @node cell-error, cell-error-name, error (Condition Type), Conditions Dictionary
  723. @subsection cell-error [Condition Type]
  724. @subsubheading Class Precedence List::
  725. @b{cell-error},
  726. @b{error},
  727. @b{serious-condition},
  728. @b{condition},
  729. @b{t}
  730. @subsubheading Description::
  731. The @i{type} @b{cell-error} consists of error conditions that occur during
  732. a location @i{access}. The name of the offending cell is initialized by
  733. the @t{:name} initialization argument to @b{make-condition},
  734. and is @i{accessed} by the @i{function} @b{cell-error-name}.
  735. @subsubheading See Also::
  736. @ref{cell-error-name}
  737. @node cell-error-name, parse-error, cell-error, Conditions Dictionary
  738. @subsection cell-error-name [Function]
  739. @code{cell-error-name} @i{condition} @result{} @i{name}
  740. @subsubheading Arguments and Values::
  741. @i{condition}---a @i{condition} of @i{type} @b{cell-error}.
  742. @i{name}---an @i{object}.
  743. @subsubheading Description::
  744. Returns the @i{name} of the offending cell involved in the @i{situation}
  745. represented by @i{condition}.
  746. The nature of the result depends on the specific @i{type} of @i{condition}.
  747. For example,
  748. if the @i{condition} is of @i{type} @b{unbound-variable}, the result is
  749. the @i{name} of the @i{unbound variable} which was being @i{accessed},
  750. if the @i{condition} is of @i{type} @b{undefined-function}, this is
  751. the @i{name} of the @i{undefined function} which was being @i{accessed},
  752. and if the @i{condition} is of @i{type} @b{unbound-slot}, this is
  753. the @i{name} of the @i{slot} which was being @i{accessed}.
  754. @subsubheading See Also::
  755. @b{cell-error},
  756. @b{unbound-slot},
  757. @b{unbound-variable},
  758. @b{undefined-function},
  759. @ref{Condition System Concepts}
  760. @node parse-error, storage-condition, cell-error-name, Conditions Dictionary
  761. @subsection parse-error [Condition Type]
  762. @subsubheading Class Precedence List::
  763. @b{parse-error},
  764. @b{error},
  765. @b{serious-condition},
  766. @b{condition},
  767. @b{t}
  768. @subsubheading Description::
  769. The @i{type} @b{parse-error} consists of
  770. error conditions that are related to parsing.
  771. @subsubheading See Also::
  772. @ref{parse-namestring}
  773. ,
  774. @ref{reader-error}
  775. @node storage-condition, assert, parse-error, Conditions Dictionary
  776. @subsection storage-condition [Condition Type]
  777. @subsubheading Class Precedence List::
  778. @b{storage-condition},
  779. @b{serious-condition},
  780. @b{condition},
  781. @b{t}
  782. @subsubheading Description::
  783. The @i{type} @b{storage-condition} consists of serious conditions that
  784. relate to problems with memory management that are potentially due to
  785. @i{implementation-dependent} limits rather than semantic errors
  786. in @i{conforming programs}, and that typically warrant entry to the
  787. debugger if not handled. Depending on the details of the @i{implementation},
  788. these might include such problems as
  789. stack overflow,
  790. memory region overflow,
  791. and
  792. storage exhausted.
  793. @subsubheading Notes::
  794. While some @r{Common Lisp} operations might signal @i{storage-condition}
  795. because they are defined to create @i{objects},
  796. it is unspecified whether operations that are not defined to create
  797. @i{objects} create them anyway
  798. and so might also signal @b{storage-condition}.
  799. Likewise, the evaluator itself might create @i{objects}
  800. and so might signal @b{storage-condition}.
  801. (The natural assumption might be that such
  802. @i{object} creation is naturally inefficient,
  803. but even that is @i{implementation-dependent}.)
  804. In general, the entire question of how storage allocation is done is
  805. @i{implementation-dependent},
  806. and so any operation might signal @b{storage-condition} at any time.
  807. Because such a @i{condition} is indicative of a limitation
  808. of the @i{implementation}
  809. or of the @i{image}
  810. rather than an error in a @i{program},
  811. @i{objects} of @i{type} @b{storage-condition} are not of @i{type} @b{error}.
  812. @node assert, error, storage-condition, Conditions Dictionary
  813. @subsection assert [Macro]
  814. @code{assert} @i{test-form @r{[}@r{(}@{@i{place}@}{*}@r{)}
  815. @r{[}datum-form
  816. @{@i{argument-form}@}{*}@r{]}@r{]}}@*
  817. @result{} @i{@b{nil}}
  818. @subsubheading Arguments and Values::
  819. @i{test-form}---a @i{form}; always evaluated.
  820. @i{place}---a @i{place}; evaluated if an error is signaled.
  821. @i{datum-form}---a @i{form} that evaluates to a @i{datum}.
  822. Evaluated each time an error is to be signaled,
  823. or not at all if no error is to be signaled.
  824. @i{argument-form}---a @i{form} that evaluates to an @i{argument}.
  825. Evaluated each time an error is to be signaled,
  826. or not at all if no error is to be signaled.
  827. @i{datum}, @i{arguments}---@i{designators} for a @i{condition}
  828. of default type @b{error}. (These @i{designators} are the
  829. result of evaluating @i{datum-form} and each of the @i{argument-forms}.)
  830. @subsubheading Description::
  831. @b{assert} assures that @i{test-form} evaluates to @i{true}.
  832. If @i{test-form} evaluates to @i{false}, @b{assert} signals a
  833. @i{correctable} @i{error} (denoted by @i{datum} and @i{arguments}).
  834. Continuing from this error using the @b{continue} @i{restart} makes it possible
  835. for the user to alter the values of the @i{places} before
  836. @b{assert} evaluates @i{test-form} again.
  837. If the value of @i{test-form} is @i{non-nil},
  838. @b{assert} returns @b{nil}.
  839. The @i{places} are @i{generalized references} to data
  840. upon which @i{test-form} depends,
  841. whose values can be changed by the user in attempting to correct the error.
  842. @i{Subforms} of each @i{place} are only evaluated if an error is signaled,
  843. and might be re-evaluated if the error is re-signaled (after continuing without
  844. actually fixing the problem).
  845. The order of evaluation of the @i{places} is not specified;
  846. see @ref{Evaluation of Subforms to Places}.
  847. @ITindex{order of evaluation}
  848. @ITindex{evaluation order}
  849. If a @i{place} @i{form} is supplied that produces more values than there
  850. are store variables, the extra values are ignored. If the supplied
  851. @i{form} produces fewer values than there are store variables,
  852. the missing values are set to @b{nil}.
  853. @subsubheading Examples::
  854. @example
  855. (setq x (make-array '(3 5) :initial-element 3))
  856. @result{} #2A((3 3 3 3 3) (3 3 3 3 3) (3 3 3 3 3))
  857. (setq y (make-array '(3 5) :initial-element 7))
  858. @result{} #2A((7 7 7 7 7) (7 7 7 7 7) (7 7 7 7 7))
  859. (defun matrix-multiply (a b)
  860. (let ((*print-array* nil))
  861. (assert (and (= (array-rank a) (array-rank b) 2)
  862. (= (array-dimension a 1) (array-dimension b 0)))
  863. (a b)
  864. "Cannot multiply ~S by ~S." a b)
  865. (really-matrix-multiply a b))) @result{} MATRIX-MULTIPLY
  866. (matrix-multiply x y)
  867. @t{ |> } Correctable error in MATRIX-MULTIPLY:
  868. @t{ |> } Cannot multiply #<ARRAY ...> by #<ARRAY ...>.
  869. @t{ |> } Restart options:
  870. @t{ |> } 1: You will be prompted for one or more new values.
  871. @t{ |> } 2: Top level.
  872. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  873. @t{ |> } Value for A: @b{|>>}@t{x}@b{<<|}
  874. @t{ |> } Value for B: @b{|>>}@t{(make-array '(5 3) :initial-element 6)}@b{<<|}
  875. @result{} #2A((54 54 54 54 54)
  876. (54 54 54 54 54)
  877. (54 54 54 54 54)
  878. (54 54 54 54 54)
  879. (54 54 54 54 54))
  880. @end example
  881. @example
  882. (defun double-safely (x) (assert (numberp x) (x)) (+ x x))
  883. (double-safely 4)
  884. @result{} 8
  885. (double-safely t)
  886. @t{ |> } Correctable error in DOUBLE-SAFELY: The value of (NUMBERP X) must be non-NIL.
  887. @t{ |> } Restart options:
  888. @t{ |> } 1: You will be prompted for one or more new values.
  889. @t{ |> } 2: Top level.
  890. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  891. @t{ |> } Value for X: @b{|>>}@t{7}@b{<<|}
  892. @result{} 14
  893. @end example
  894. @subsubheading Affected By::
  895. @b{*break-on-signals*}
  896. The set of active @i{condition handlers}.
  897. @subsubheading See Also::
  898. @ref{check-type}
  899. ,
  900. @ref{error}
  901. , @ref{Generalized Reference}
  902. @subsubheading Notes::
  903. The debugger need not include the @i{test-form} in the error message,
  904. and the @i{places} should not be included in the message, but they
  905. should be made available for the user's perusal. If the user gives the
  906. ``continue'' command, the values of any of the references can be altered.
  907. The details of this depend on the implementation's style of user interface.
  908. @node error, cerror, assert, Conditions Dictionary
  909. @subsection error [Function]
  910. @code{error} @i{datum {&rest} arguments}
  911. @result{} #<NoValue>
  912. @subsubheading Arguments and Values::
  913. @i{datum}, @i{arguments}---@i{designators} for a @i{condition}
  914. of default type @b{simple-error}.
  915. @subsubheading Description::
  916. @b{error} effectively invokes @b{signal} on the denoted @i{condition}.
  917. If the @i{condition} is not handled, @t{(invoke-debugger @i{condition})} is done.
  918. As a consequence of calling @b{invoke-debugger}, @b{error}
  919. cannot directly return; the only exit from @b{error}
  920. can come by non-local transfer of control in a handler or by use of
  921. an interactive debugging command.
  922. @subsubheading Examples::
  923. @example
  924. (defun factorial (x)
  925. (cond ((or (not (typep x 'integer)) (minusp x))
  926. (error "~S is not a valid argument to FACTORIAL." x))
  927. ((zerop x) 1)
  928. (t (* x (factorial (- x 1))))))
  929. @result{} FACTORIAL
  930. (factorial 20)
  931. @result{} 2432902008176640000
  932. (factorial -1)
  933. @t{ |> } Error: -1 is not a valid argument to FACTORIAL.
  934. @t{ |> } To continue, type :CONTINUE followed by an option number:
  935. @t{ |> } 1: Return to Lisp Toplevel.
  936. @t{ |> } Debug>
  937. @end example
  938. @example
  939. (setq a 'fred)
  940. @result{} FRED
  941. (if (numberp a) (1+ a) (error "~S is not a number." A))
  942. @t{ |> } Error: FRED is not a number.
  943. @t{ |> } To continue, type :CONTINUE followed by an option number:
  944. @t{ |> } 1: Return to Lisp Toplevel.
  945. @t{ |> } Debug> @b{|>>}@t{:Continue 1}@b{<<|}
  946. @t{ |> } Return to Lisp Toplevel.
  947. (define-condition not-a-number (error)
  948. ((argument :reader not-a-number-argument :initarg :argument))
  949. (:report (lambda (condition stream)
  950. (format stream "~S is not a number."
  951. (not-a-number-argument condition)))))
  952. @result{} NOT-A-NUMBER
  953. (if (numberp a) (1+ a) (error 'not-a-number :argument a))
  954. @t{ |> } Error: FRED is not a number.
  955. @t{ |> } To continue, type :CONTINUE followed by an option number:
  956. @t{ |> } 1: Return to Lisp Toplevel.
  957. @t{ |> } Debug> @b{|>>}@t{:Continue 1}@b{<<|}
  958. @t{ |> } Return to Lisp Toplevel.
  959. @end example
  960. @subsubheading Side Effects::
  961. @i{Handlers} for the specified condition, if any, are invoked
  962. and might have side effects.
  963. Program execution might stop, and the debugger might be entered.
  964. @subsubheading Affected By::
  965. Existing handler bindings.
  966. @b{*break-on-signals*}
  967. Signals an error of @i{type} @b{type-error} if @i{datum} and @i{arguments} are not @i{designators} for a @i{condition}.
  968. @subsubheading See Also::
  969. @ref{cerror}
  970. ,
  971. @ref{signal}
  972. ,
  973. @ref{format}
  974. ,
  975. @ref{ignore-errors}
  976. , @b{*break-on-signals*},
  977. @ref{handler-bind}
  978. , @ref{Condition System Concepts}
  979. @subsubheading Notes::
  980. Some implementations may provide debugger
  981. commands for interactively returning from individual stack frames.
  982. However, it should be possible for the programmer to feel confident
  983. about writing code like:
  984. @example
  985. (defun wargames:no-win-scenario ()
  986. (if (error "pushing the button would be stupid."))
  987. (push-the-button))
  988. @end example
  989. In this scenario, there should be no chance that
  990. @b{error} will return
  991. and the button will get pushed.
  992. While the meaning of this program is clear and it might be proven `safe'
  993. by a formal theorem prover, such a proof is no guarantee that the
  994. program is safe to execute. Compilers have been known to have bugs,
  995. computers to have signal glitches, and human beings to manually
  996. intervene in ways that are not always possible to predict. Those kinds
  997. of errors, while beyond the scope of the condition system to formally
  998. model, are not beyond the scope of things that should seriously be
  999. considered when writing code that could have the kinds of sweeping
  1000. effects hinted at by this example.
  1001. @node cerror, check-type, error, Conditions Dictionary
  1002. @subsection cerror [Function]
  1003. @code{cerror} @i{continue-format-control datum {&rest} arguments} @result{} @i{@b{nil}}
  1004. @subsubheading Arguments and Values::
  1005. @i{Continue-format-control}---a @i{format control}.
  1006. [Reviewer Note by Barmar: What is continue-format-control used for??]
  1007. @i{datum}, @i{arguments}---@i{designators} for a @i{condition}
  1008. of default type @b{simple-error}.
  1009. @subsubheading Description::
  1010. @b{cerror} effectively invokes @b{error} on the
  1011. @i{condition} named by @i{datum}. As with any function that
  1012. implicitly calls @b{error}, if the @i{condition} is not handled,
  1013. @t{(invoke-debugger @i{condition})} is executed. While signaling is going on,
  1014. and while in the debugger if it is reached, it is possible to continue
  1015. code execution (@i{i.e.}, to return from @b{cerror}) using the @b{continue} @i{restart}.
  1016. If @i{datum} is a @i{condition}, @i{arguments} can be supplied,
  1017. but are used only in conjunction with the @i{continue-format-control}.
  1018. @subsubheading Examples::
  1019. @example
  1020. (defun real-sqrt (n)
  1021. (when (minusp n)
  1022. (setq n (- n))
  1023. (cerror "Return sqrt(~D) instead." "Tried to take sqrt(-~D)." n))
  1024. (sqrt n))
  1025. (real-sqrt 4)
  1026. @result{} 2.0
  1027. (real-sqrt -9)
  1028. @t{ |> } Correctable error in REAL-SQRT: Tried to take sqrt(-9).
  1029. @t{ |> } Restart options:
  1030. @t{ |> } 1: Return sqrt(9) instead.
  1031. @t{ |> } 2: Top level.
  1032. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  1033. @result{} 3.0
  1034. (define-condition not-a-number (error)
  1035. ((argument :reader not-a-number-argument :initarg :argument))
  1036. (:report (lambda (condition stream)
  1037. (format stream "~S is not a number."
  1038. (not-a-number-argument condition)))))
  1039. (defun assure-number (n)
  1040. (loop (when (numberp n) (return n))
  1041. (cerror "Enter a number."
  1042. 'not-a-number :argument n)
  1043. (format t "~&Type a number: ")
  1044. (setq n (read))
  1045. (fresh-line)))
  1046. (assure-number 'a)
  1047. @t{ |> } Correctable error in ASSURE-NUMBER: A is not a number.
  1048. @t{ |> } Restart options:
  1049. @t{ |> } 1: Enter a number.
  1050. @t{ |> } 2: Top level.
  1051. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  1052. @t{ |> } Type a number: @b{|>>}@t{1/2}@b{<<|}
  1053. @result{} 1/2
  1054. (defun assure-large-number (n)
  1055. (loop (when (and (numberp n) (> n 73)) (return n))
  1056. (cerror "Enter a number~:[~; a bit larger than ~D~]."
  1057. "~*~A is not a large number."
  1058. (numberp n) n)
  1059. (format t "~&Type a large number: ")
  1060. (setq n (read))
  1061. (fresh-line)))
  1062. (assure-large-number 10000)
  1063. @result{} 10000
  1064. (assure-large-number 'a)
  1065. @t{ |> } Correctable error in ASSURE-LARGE-NUMBER: A is not a large number.
  1066. @t{ |> } Restart options:
  1067. @t{ |> } 1: Enter a number.
  1068. @t{ |> } 2: Top level.
  1069. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  1070. @t{ |> } Type a large number: @b{|>>}@t{88}@b{<<|}
  1071. @result{} 88
  1072. (assure-large-number 37)
  1073. @t{ |> } Correctable error in ASSURE-LARGE-NUMBER: 37 is not a large number.
  1074. @t{ |> } Restart options:
  1075. @t{ |> } 1: Enter a number a bit larger than 37.
  1076. @t{ |> } 2: Top level.
  1077. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  1078. @t{ |> } Type a large number: @b{|>>}@t{259}@b{<<|}
  1079. @result{} 259
  1080. (define-condition not-a-large-number (error)
  1081. ((argument :reader not-a-large-number-argument :initarg :argument))
  1082. (:report (lambda (condition stream)
  1083. (format stream "~S is not a large number."
  1084. (not-a-large-number-argument condition)))))
  1085. (defun assure-large-number (n)
  1086. (loop (when (and (numberp n) (> n 73)) (return n))
  1087. (cerror "Enter a number~3*~:[~; a bit larger than ~*~D~]."
  1088. 'not-a-large-number
  1089. :argument n
  1090. :ignore (numberp n)
  1091. :ignore n
  1092. :allow-other-keys t)
  1093. (format t "~&Type a large number: ")
  1094. (setq n (read))
  1095. (fresh-line)))
  1096. (assure-large-number 'a)
  1097. @t{ |> } Correctable error in ASSURE-LARGE-NUMBER: A is not a large number.
  1098. @t{ |> } Restart options:
  1099. @t{ |> } 1: Enter a number.
  1100. @t{ |> } 2: Top level.
  1101. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  1102. @t{ |> } Type a large number: @b{|>>}@t{88}@b{<<|}
  1103. @result{} 88
  1104. (assure-large-number 37)
  1105. @t{ |> } Correctable error in ASSURE-LARGE-NUMBER: A is not a large number.
  1106. @t{ |> } Restart options:
  1107. @t{ |> } 1: Enter a number a bit larger than 37.
  1108. @t{ |> } 2: Top level.
  1109. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  1110. @t{ |> } Type a large number: @b{|>>}@t{259}@b{<<|}
  1111. @result{} 259
  1112. @end example
  1113. @subsubheading Affected By::
  1114. @b{*break-on-signals*}.
  1115. Existing handler bindings.
  1116. @subsubheading See Also::
  1117. @ref{error}
  1118. ,
  1119. @ref{format}
  1120. ,
  1121. @ref{handler-bind}
  1122. ,
  1123. @b{*break-on-signals*}, @b{simple-type-error}
  1124. @subsubheading Notes::
  1125. If @i{datum} is a @i{condition} @i{type} rather than a
  1126. @i{string}, the @b{format} directive @t{~*} may be especially
  1127. useful in the @i{continue-format-control} in order to ignore the
  1128. @i{keywords} in the @i{initialization argument list}. For example:
  1129. @example
  1130. (cerror "enter a new value to replace ~*~s"
  1131. 'not-a-number
  1132. :argument a)
  1133. @end example
  1134. @node check-type, simple-error, cerror, Conditions Dictionary
  1135. @subsection check-type [Macro]
  1136. @code{check-type} @i{place typespec {@r{[}@i{string}@r{]}}} @result{} @i{@b{nil}}
  1137. @subsubheading Arguments and Values::
  1138. @i{place}---a @i{place}.
  1139. @i{typespec}---a @i{type specifier}.
  1140. @i{string}---a @i{string}; evaluated.
  1141. @subsubheading Description::
  1142. @b{check-type} signals a @i{correctable} @i{error}
  1143. of @i{type} @b{type-error} if the contents of @i{place} are not
  1144. of the type @i{typespec}.
  1145. @b{check-type} can return only if the @b{store-value} @i{restart} is invoked,
  1146. either explicitly from a handler
  1147. or implicitly as one of the options offered by the debugger.
  1148. If the @b{store-value} @i{restart} is invoked,
  1149. @b{check-type} stores the new value
  1150. that is the argument to the @i{restart} invocation
  1151. (or that is prompted for interactively by the debugger)
  1152. in @i{place} and starts over,
  1153. checking the type of the new value
  1154. and signaling another error if it is still not of the desired @i{type}.
  1155. The first time @i{place} is @i{evaluated},
  1156. it is @i{evaluated} by normal evaluation rules.
  1157. It is later @i{evaluated} as a @i{place}
  1158. if the type check fails and the @b{store-value} @i{restart} is used;
  1159. see @ref{Evaluation of Subforms to Places}.
  1160. @i{string} should be an English description of the type,
  1161. starting with an indefinite article (``a'' or ``an'').
  1162. If @i{string} is not supplied,
  1163. it is computed automatically from @i{typespec}.
  1164. The automatically generated message mentions
  1165. @i{place},
  1166. its contents,
  1167. and the desired type.
  1168. An implementation may choose to generate
  1169. a somewhat differently worded error message
  1170. if it recognizes that @i{place} is of a particular form,
  1171. such as one of the arguments to the function that called @b{check-type}.
  1172. @i{string} is allowed because some applications of @b{check-type}
  1173. may require a more specific description of what is wanted
  1174. than can be generated automatically from @i{typespec}.
  1175. @subsubheading Examples::
  1176. @example
  1177. (setq aardvarks '(sam harry fred))
  1178. @result{} (SAM HARRY FRED)
  1179. (check-type aardvarks (array * (3)))
  1180. @t{ |> } Error: The value of AARDVARKS, (SAM HARRY FRED),
  1181. @t{ |> } is not a 3-long array.
  1182. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1183. @t{ |> } 1: Specify a value to use instead.
  1184. @t{ |> } 2: Return to Lisp Toplevel.
  1185. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 1}@b{<<|}
  1186. @t{ |> } Use Value: @b{|>>}@t{#(SAM FRED HARRY)}@b{<<|}
  1187. @result{} NIL
  1188. aardvarks
  1189. @result{} #<ARRAY-T-3 13571>
  1190. (map 'list #'identity aardvarks)
  1191. @result{} (SAM FRED HARRY)
  1192. (setq aardvark-count 'foo)
  1193. @result{} FOO
  1194. (check-type aardvark-count (integer 0 *) "A positive integer")
  1195. @t{ |> } Error: The value of AARDVARK-COUNT, FOO, is not a positive integer.
  1196. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1197. @t{ |> } 1: Specify a value to use instead.
  1198. @t{ |> } 2: Top level.
  1199. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 2}@b{<<|}
  1200. @end example
  1201. @example
  1202. (defmacro define-adder (name amount)
  1203. (check-type name (and symbol (not null)) "a name for an adder function")
  1204. (check-type amount integer)
  1205. `(defun ,name (x) (+ x ,amount)))
  1206. (macroexpand '(define-adder add3 3))
  1207. @result{} (defun add3 (x) (+ x 3))
  1208. (macroexpand '(define-adder 7 7))
  1209. @t{ |> } Error: The value of NAME, 7, is not a name for an adder function.
  1210. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1211. @t{ |> } 1: Specify a value to use instead.
  1212. @t{ |> } 2: Top level.
  1213. @t{ |> } Debug> @b{|>>}@t{:Continue 1}@b{<<|}
  1214. @t{ |> } Specify a value to use instead.
  1215. @t{ |> } Type a form to be evaluated and used instead: @b{|>>}@t{'ADD7}@b{<<|}
  1216. @result{} (defun add7 (x) (+ x 7))
  1217. (macroexpand '(define-adder add5 something))
  1218. @t{ |> } Error: The value of AMOUNT, SOMETHING, is not an integer.
  1219. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1220. @t{ |> } 1: Specify a value to use instead.
  1221. @t{ |> } 2: Top level.
  1222. @t{ |> } Debug> @b{|>>}@t{:Continue 1}@b{<<|}
  1223. @t{ |> } Type a form to be evaluated and used instead: @b{|>>}@t{5}@b{<<|}
  1224. @result{} (defun add5 (x) (+ x 5))
  1225. @end example
  1226. Control is transferred to a handler.
  1227. @subsubheading Side Effects::
  1228. The debugger might be entered.
  1229. @subsubheading Affected By::
  1230. @b{*break-on-signals*}
  1231. The implementation.
  1232. @subsubheading See Also::
  1233. @ref{Condition System Concepts}
  1234. @subsubheading Notes::
  1235. @example
  1236. (check-type @i{place} @i{typespec})
  1237. @equiv{} (assert (typep @i{place} '@i{typespec}) (@i{place})
  1238. 'type-error :datum @i{place} :expected-type '@i{typespec})
  1239. @end example
  1240. @node simple-error, invalid-method-error, check-type, Conditions Dictionary
  1241. @subsection simple-error [Condition Type]
  1242. @subsubheading Class Precedence List::
  1243. @b{simple-error},
  1244. @b{simple-condition},
  1245. @b{error},
  1246. @b{serious-condition},
  1247. @b{condition},
  1248. @b{t}
  1249. @subsubheading Description::
  1250. The @i{type} @b{simple-error} consists of @i{conditions} that
  1251. are signaled by @b{error} or @b{cerror} when a
  1252. @i{format control}
  1253. is supplied as the function's first argument.
  1254. @node invalid-method-error, method-combination-error, simple-error, Conditions Dictionary
  1255. @subsection invalid-method-error [Function]
  1256. @code{invalid-method-error} @i{method format-control {&rest} args} @result{} @i{@i{implementation-dependent}}
  1257. @subsubheading Arguments and Values::
  1258. @i{method}---a @i{method}.
  1259. @i{format-control}---a @i{format control}.
  1260. @i{args}---@i{format arguments} for the @i{format-control}.
  1261. @subsubheading Description::
  1262. The @i{function} @b{invalid-method-error} is used to signal an error of @i{type} @b{error}
  1263. when there is an applicable @i{method} whose @i{qualifiers} are not valid for
  1264. the method combination type. The error message is constructed by
  1265. using the @i{format-control} suitable for @b{format}
  1266. and any @i{args} to it. Because an
  1267. implementation may need to add additional contextual information to
  1268. the error message, @b{invalid-method-error} should be called only
  1269. within the dynamic extent of a method combination function.
  1270. The @i{function} @b{invalid-method-error} is called automatically when a
  1271. @i{method} fails to satisfy every @i{qualifier} pattern and predicate in a
  1272. @b{define-method-combination} @i{form}. A method combination function
  1273. that imposes additional restrictions should call
  1274. @b{invalid-method-error} explicitly if it encounters a @i{method}
  1275. it cannot accept.
  1276. Whether @b{invalid-method-error} returns to its caller or exits via
  1277. @b{throw} is @i{implementation-dependent}.
  1278. @subsubheading Side Effects::
  1279. The debugger might be entered.
  1280. @subsubheading Affected By::
  1281. @b{*break-on-signals*}
  1282. @subsubheading See Also::
  1283. @ref{define-method-combination}
  1284. @node method-combination-error, signal, invalid-method-error, Conditions Dictionary
  1285. @subsection method-combination-error [Function]
  1286. @code{method-combination-error} @i{format-control {&rest} args} @result{} @i{@i{implementation-dependent}}
  1287. @subsubheading Arguments and Values::
  1288. @i{format-control}---a @i{format control}.
  1289. @i{args}---@i{format arguments} for @i{format-control}.
  1290. @subsubheading Description::
  1291. The @i{function} @b{method-combination-error} is used to signal an error
  1292. in method combination.
  1293. The error message is constructed by using a @i{format-control} suitable
  1294. for @b{format} and any @i{args} to it. Because an implementation may
  1295. need to add additional contextual information to the error message,
  1296. @b{method-combination-error} should be called only within the
  1297. dynamic extent of a method combination function.
  1298. Whether @b{method-combination-error} returns to its caller or exits
  1299. via @b{throw} is @i{implementation-dependent}.
  1300. @subsubheading Side Effects::
  1301. The debugger might be entered.
  1302. @subsubheading Affected By::
  1303. @b{*break-on-signals*}
  1304. @subsubheading See Also::
  1305. @ref{define-method-combination}
  1306. @node signal, simple-condition, method-combination-error, Conditions Dictionary
  1307. @subsection signal [Function]
  1308. @code{signal} @i{datum {&rest} arguments} @result{} @i{@b{nil}}
  1309. @subsubheading Arguments and Values::
  1310. @i{datum}, @i{arguments}---@i{designators} for a @i{condition}
  1311. of default type @b{simple-condition}.
  1312. @subsubheading Description::
  1313. @i{Signals} the @i{condition} denoted by the given @i{datum} and @i{arguments}.
  1314. If the @i{condition} is not handled, @b{signal} returns @b{nil}.
  1315. @subsubheading Examples::
  1316. @example
  1317. (defun handle-division-conditions (condition)
  1318. (format t "Considering condition for division condition handling~
  1319. (when (and (typep condition 'arithmetic-error)
  1320. (eq '/ (arithmetic-error-operation condition)))
  1321. (invoke-debugger condition)))
  1322. HANDLE-DIVISION-CONDITIONS
  1323. (defun handle-other-arithmetic-errors (condition)
  1324. (format t "Considering condition for arithmetic condition handling~
  1325. (when (typep condition 'arithmetic-error)
  1326. (abort)))
  1327. HANDLE-OTHER-ARITHMETIC-ERRORS
  1328. (define-condition a-condition-with-no-handler (condition) ())
  1329. A-CONDITION-WITH-NO-HANDLER
  1330. (signal 'a-condition-with-no-handler)
  1331. NIL
  1332. (handler-bind ((condition #'handle-division-conditions)
  1333. (condition #'handle-other-arithmetic-errors))
  1334. (signal 'a-condition-with-no-handler))
  1335. Considering condition for division condition handling
  1336. Considering condition for arithmetic condition handling
  1337. NIL
  1338. (handler-bind ((arithmetic-error #'handle-division-conditions)
  1339. (arithmetic-error #'handle-other-arithmetic-errors))
  1340. (signal 'arithmetic-error :operation '* :operands '(1.2 b)))
  1341. Considering condition for division condition handling
  1342. Considering condition for arithmetic condition handling
  1343. Back to Lisp Toplevel
  1344. @end example
  1345. @subsubheading Side Effects::
  1346. The debugger might be entered due to @b{*break-on-signals*}.
  1347. Handlers for the condition being signaled might transfer control.
  1348. @subsubheading Affected By::
  1349. Existing handler bindings.
  1350. @b{*break-on-signals*}
  1351. @subsubheading See Also::
  1352. @b{*break-on-signals*},
  1353. @ref{error}
  1354. ,
  1355. @b{simple-condition},
  1356. @ref{Signaling and Handling Conditions}
  1357. @subsubheading Notes::
  1358. If @t{(typep @i{datum} *break-on-signals*)} @i{yields} @i{true},
  1359. the debugger is entered prior to beginning the signaling process.
  1360. The @b{continue} @i{restart} can be used to continue with the signaling process.
  1361. This is also true for all other @i{functions} and @i{macros} that
  1362. should, might, or must @i{signal} @i{conditions}.
  1363. @node simple-condition, simple-condition-format-control, signal, Conditions Dictionary
  1364. @subsection simple-condition [Condition Type]
  1365. @subsubheading Class Precedence List::
  1366. @b{simple-condition},
  1367. @b{condition},
  1368. @b{t}
  1369. @subsubheading Description::
  1370. The @i{type} @b{simple-condition} represents @i{conditions} that are
  1371. signaled by @b{signal} whenever a @i{format-control} is
  1372. supplied as the function's first argument.
  1373. The @i{format control} and @i{format arguments} are initialized with
  1374. the initialization arguments named @t{:format-control}
  1375. and @t{:format-arguments} to @b{make-condition}, and are
  1376. @i{accessed} by the @i{functions}
  1377. @b{simple-condition-format-control}
  1378. and @b{simple-condition-format-arguments}.
  1379. If format arguments are not supplied to @b{make-condition},
  1380. @b{nil} is used as a default.
  1381. @subsubheading See Also::
  1382. @ref{simple-condition-format-control; simple-condition-format-arguments}
  1383. ,
  1384. @b{simple-condition-format-arguments}
  1385. @node simple-condition-format-control, warn, simple-condition, Conditions Dictionary
  1386. @subsection simple-condition-format-control, simple-condition-format-arguments
  1387. @flushright
  1388. @i{[Function]}
  1389. @end flushright
  1390. @code{simple-condition-format-control} @i{condition} @result{} @i{format-control}
  1391. @code{simple-condition-format-arguments} @i{condition} @result{} @i{format-arguments}
  1392. @subsubheading Arguments and Values::
  1393. @i{condition}---a @i{condition} of @i{type} @b{simple-condition}.
  1394. @i{format-control}---a @i{format control}.
  1395. @i{format-arguments}---a @i{list}.
  1396. @subsubheading Description::
  1397. @b{simple-condition-format-control} returns the @i{format control} needed to
  1398. process the @i{condition}'s @i{format arguments}.
  1399. @b{simple-condition-format-arguments} returns a @i{list} of @i{format arguments}
  1400. needed to process the @i{condition}'s @i{format control}.
  1401. @subsubheading Examples::
  1402. @example
  1403. (setq foo (make-condition 'simple-condition
  1404. :format-control "Hi ~S"
  1405. :format-arguments '(ho)))
  1406. @result{} #<SIMPLE-CONDITION 26223553>
  1407. (apply #'format nil (simple-condition-format-control foo)
  1408. (simple-condition-format-arguments foo))
  1409. @result{} "Hi HO"
  1410. @end example
  1411. @subsubheading See Also::
  1412. @ref{simple-condition}
  1413. ,
  1414. @ref{Condition System Concepts}
  1415. @node warn, simple-warning, simple-condition-format-control, Conditions Dictionary
  1416. @subsection warn [Function]
  1417. @code{warn} @i{datum {&rest} arguments} @result{} @i{@b{nil}}
  1418. @subsubheading Arguments and Values::
  1419. @i{datum}, @i{arguments}---@i{designators} for a @i{condition}
  1420. of default type @b{simple-warning}.
  1421. @subsubheading Description::
  1422. @i{Signals} a @i{condition} of @i{type} @b{warning}.
  1423. If the @i{condition} is not @i{handled},
  1424. reports the @i{condition} to @i{error output}.
  1425. The precise mechanism for warning is as follows:
  1426. @table @asis
  1427. @item @b{The warning condition is signaled}
  1428. While the @b{warning} @i{condition} is being signaled,
  1429. the @b{muffle-warning} @i{restart} is established for use by a @i{handler}.
  1430. If invoked, this @i{restart} bypasses further action by @b{warn},
  1431. which in turn causes @b{warn} to immediately return @b{nil}.
  1432. @item @b{If no handler for the warning condition is found}
  1433. If no handlers for the warning condition are found,
  1434. or if all such handlers decline,
  1435. then the @i{condition} is reported to @i{error output}
  1436. by @b{warn} in an @i{implementation-dependent} format.
  1437. @item @b{@b{nil} is returned}
  1438. The value returned by @b{warn} if it returns is @b{nil}.
  1439. @end table
  1440. @subsubheading Examples::
  1441. @example
  1442. (defun foo (x)
  1443. (let ((result (* x 2)))
  1444. (if (not (typep result 'fixnum))
  1445. (warn "You're using very big numbers."))
  1446. result))
  1447. @result{} FOO
  1448. (foo 3)
  1449. @result{} 6
  1450. (foo most-positive-fixnum)
  1451. @t{ |> } Warning: You're using very big numbers.
  1452. @result{} 4294967294
  1453. (setq *break-on-signals* t)
  1454. @result{} T
  1455. (foo most-positive-fixnum)
  1456. @t{ |> } Break: Caveat emptor.
  1457. @t{ |> } To continue, type :CONTINUE followed by an option number.
  1458. @t{ |> } 1: Return from Break.
  1459. @t{ |> } 2: Abort to Lisp Toplevel.
  1460. @t{ |> } Debug> :continue 1
  1461. @t{ |> } Warning: You're using very big numbers.
  1462. @result{} 4294967294
  1463. @end example
  1464. @subsubheading Side Effects::
  1465. A warning is issued. The debugger might be entered.
  1466. @subsubheading Affected By::
  1467. Existing handler bindings.
  1468. @b{*break-on-signals*},
  1469. @b{*error-output*}.
  1470. @subsubheading Exceptional Situations::
  1471. If @i{datum} is a @i{condition}
  1472. and if the @i{condition} is not of @i{type} @b{warning},
  1473. or @i{arguments} is @i{non-nil}, an error of @i{type} @b{type-error} is signaled.
  1474. If @i{datum} is a condition type,
  1475. the result of @t{(apply #'make-condition datum arguments)}
  1476. must be of @i{type} @b{warning} or an error of @i{type} @b{type-error} is signaled.
  1477. @subsubheading See Also::
  1478. @b{*break-on-signals*},
  1479. @ref{muffle-warning}
  1480. ,
  1481. @ref{signal}
  1482. @node simple-warning, invoke-debugger, warn, Conditions Dictionary
  1483. @subsection simple-warning [Condition Type]
  1484. @subsubheading Class Precedence List::
  1485. @b{simple-warning},
  1486. @b{simple-condition},
  1487. @b{warning},
  1488. @b{condition},
  1489. @b{t}
  1490. @subsubheading Description::
  1491. The @i{type} @b{simple-warning} represents @i{conditions} that
  1492. are signaled by @b{warn} whenever a
  1493. @i{format control}
  1494. is supplied as the function's first argument.
  1495. @node invoke-debugger, break, simple-warning, Conditions Dictionary
  1496. @subsection invoke-debugger [Function]
  1497. @code{invoke-debugger} @i{condition}
  1498. @result{} #<NoValue>
  1499. @subsubheading Arguments and Values::
  1500. @i{condition}---a @i{condition} @i{object}.
  1501. @subsubheading Description::
  1502. @b{invoke-debugger} attempts to enter the debugger with @i{condition}.
  1503. If @b{*debugger-hook*} is not @b{nil}, it should be a @i{function}
  1504. (or the name of a @i{function}) to be called prior to entry to
  1505. the standard debugger. The @i{function} is called with
  1506. @b{*debugger-hook*} bound to @b{nil}, and the @i{function}
  1507. must accept two arguments: the @i{condition}
  1508. and the @i{value} of @b{*debugger-hook*} prior to binding it to @b{nil}.
  1509. If the @i{function} returns normally,
  1510. the standard debugger is entered.
  1511. The standard debugger never directly returns. Return can occur only by a
  1512. non-local transfer of control, such as the use of a restart function.
  1513. @subsubheading Examples::
  1514. @example
  1515. (ignore-errors ;Normally, this would suppress debugger entry
  1516. (handler-bind ((error #'invoke-debugger)) ;But this forces debugger entry
  1517. (error "Foo.")))
  1518. Debug: Foo.
  1519. To continue, type :CONTINUE followed by an option number:
  1520. 1: Return to Lisp Toplevel.
  1521. Debug>
  1522. @end example
  1523. @subsubheading Side Effects::
  1524. @b{*debugger-hook*} is bound to @b{nil},
  1525. program execution is discontinued,
  1526. and the debugger is entered.
  1527. @subsubheading Affected By::
  1528. @b{*debug-io*} and @b{*debugger-hook*}.
  1529. @subsubheading See Also::
  1530. @ref{error}
  1531. ,
  1532. @ref{break}
  1533. @node break, *debugger-hook*, invoke-debugger, Conditions Dictionary
  1534. @subsection break [Function]
  1535. @code{break} @i{{&optional} format-control {&rest} format-arguments} @result{} @i{@b{nil}}
  1536. @subsubheading Arguments and Values::
  1537. @i{format-control}---a @i{format control}.
  1538. The default is @i{implementation-dependent}.
  1539. @i{format-arguments}---@i{format arguments} for the @i{format-control}.
  1540. @subsubheading Description::
  1541. @b{break} @i{formats} @i{format-control} and @i{format-arguments}
  1542. and then goes directly into the debugger without allowing any possibility of
  1543. interception by programmed error-handling facilities.
  1544. If the @b{continue} @i{restart} is used while in the debugger,
  1545. @b{break} immediately returns @b{nil} without taking any unusual recovery action.
  1546. @b{break} binds @b{*debugger-hook*} to @b{nil}
  1547. before attempting to enter the debugger.
  1548. @subsubheading Examples::
  1549. @example
  1550. (break "You got here with arguments: ~:S." '(FOO 37 A))
  1551. @t{ |> } BREAK: You got here with these arguments: FOO, 37, A.
  1552. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1553. @t{ |> } 1: Return from BREAK.
  1554. @t{ |> } 2: Top level.
  1555. @t{ |> } Debug> :CONTINUE 1
  1556. @t{ |> } Return from BREAK.
  1557. @result{} NIL
  1558. @end example
  1559. @subsubheading Side Effects::
  1560. The debugger is entered.
  1561. @subsubheading Affected By::
  1562. @b{*debug-io*}.
  1563. @subsubheading See Also::
  1564. @ref{error}
  1565. ,
  1566. @ref{invoke-debugger}
  1567. .
  1568. @subsubheading Notes::
  1569. @b{break} is used as a way of inserting temporary debugging
  1570. ``breakpoints'' in a program, not as a way of signaling errors.
  1571. For this reason, @b{break} does not take the @i{continue-format-control}
  1572. @i{argument} that @b{cerror} takes.
  1573. This and the lack of any possibility of interception by
  1574. @i{condition} @i{handling} are the only program-visible
  1575. differences between @b{break} and @b{cerror}.
  1576. The user interface aspects of @b{break} and @b{cerror} are
  1577. permitted to vary more widely, in order to accomodate the interface
  1578. needs of the @i{implementation}. For example, it is permissible for a
  1579. @i{Lisp read-eval-print loop} to be entered by @b{break} rather
  1580. than the conventional debugger.
  1581. @b{break} could be defined by:
  1582. @example
  1583. (defun break (&optional (format-control "Break") &rest format-arguments)
  1584. (with-simple-restart (continue "Return from BREAK.")
  1585. (let ((*debugger-hook* nil))
  1586. (invoke-debugger
  1587. (make-condition 'simple-condition
  1588. :format-control format-control
  1589. :format-arguments format-arguments))))
  1590. nil)
  1591. @end example
  1592. @node *debugger-hook*, *break-on-signals*, break, Conditions Dictionary
  1593. @subsection *debugger-hook* [Variable]
  1594. @subsubheading Value Type::
  1595. a @i{designator} for a @i{function} of two @i{arguments}
  1596. (a @i{condition} and the @i{value} of @b{*debugger-hook*} at the time
  1597. the debugger was entered),
  1598. or @b{nil}.
  1599. @subsubheading Initial Value::
  1600. @b{nil}.
  1601. @subsubheading Description::
  1602. When the @i{value} of @b{*debugger-hook*} is @i{non-nil}, it is called prior to
  1603. normal entry into the debugger, either due to a call to @b{invoke-debugger}
  1604. or due to automatic entry into the debugger from a call to @b{error}
  1605. or @b{cerror} with a condition that is not handled.
  1606. The @i{function} may either handle the @i{condition}
  1607. (transfer control) or return normally (allowing the standard debugger to run).
  1608. To minimize recursive errors while debugging,
  1609. @b{*debugger-hook*} is bound to @b{nil} by @b{invoke-debugger}
  1610. prior to calling the @i{function}.
  1611. @subsubheading Examples::
  1612. @example
  1613. (defun one-of (choices &optional (prompt "Choice"))
  1614. (let ((n (length choices)) (i))
  1615. (do ((c choices (cdr c)) (i 1 (+ i 1)))
  1616. ((null c))
  1617. (format t "~&[~D] ~A~
  1618. (do () ((typep i `(integer 1 ,n)))
  1619. (format t "~&~A: " prompt)
  1620. (setq i (read))
  1621. (fresh-line))
  1622. (nth (- i 1) choices)))
  1623. (defun my-debugger (condition me-or-my-encapsulation)
  1624. (format t "~&Fooey: ~A" condition)
  1625. (let ((restart (one-of (compute-restarts))))
  1626. (if (not restart) (error "My debugger got an error."))
  1627. (let ((*debugger-hook* me-or-my-encapsulation))
  1628. (invoke-restart-interactively restart))))
  1629. (let ((*debugger-hook* #'my-debugger))
  1630. (+ 3 'a))
  1631. @t{ |> } Fooey: The argument to +, A, is not a number.
  1632. @t{ |> } [1] Supply a replacement for A.
  1633. @t{ |> } [2] Return to Cloe Toplevel.
  1634. @t{ |> } Choice: 1
  1635. @t{ |> } Form to evaluate and use: (+ 5 'b)
  1636. @t{ |> } Fooey: The argument to +, B, is not a number.
  1637. @t{ |> } [1] Supply a replacement for B.
  1638. @t{ |> } [2] Supply a replacement for A.
  1639. @t{ |> } [3] Return to Cloe Toplevel.
  1640. @t{ |> } Choice: 1
  1641. @t{ |> } Form to evaluate and use: 1
  1642. @result{} 9
  1643. @end example
  1644. @subsubheading Affected By::
  1645. @b{invoke-debugger}
  1646. @subsubheading Notes::
  1647. When evaluating code typed in by the user interactively, it is sometimes
  1648. useful to have the hook function bind @b{*debugger-hook*} to the
  1649. @i{function} that was its second argument so that recursive errors
  1650. can be handled using the same interactive facility.
  1651. @node *break-on-signals*, handler-bind, *debugger-hook*, Conditions Dictionary
  1652. @subsection *break-on-signals* [Variable]
  1653. @subsubheading Value Type::
  1654. a @i{type specifier}.
  1655. @subsubheading Initial Value::
  1656. @b{nil}.
  1657. @subsubheading Description::
  1658. When @t{(typep @i{condition} *break-on-signals*)} returns @i{true},
  1659. calls to @b{signal}, and to other @i{operators} such as @b{error}
  1660. that implicitly call @b{signal}, enter the debugger prior to
  1661. @i{signaling} the @i{condition}.
  1662. The @b{continue} @i{restart} can be used to continue with the normal
  1663. @i{signaling} process when a break occurs process due to
  1664. @b{*break-on-signals*}.
  1665. @subsubheading Examples::
  1666. @example
  1667. *break-on-signals* @result{} NIL
  1668. (ignore-errors (error 'simple-error :format-control "Fooey!"))
  1669. @result{} NIL, #<SIMPLE-ERROR 32207172>
  1670. (let ((*break-on-signals* 'error))
  1671. (ignore-errors (error 'simple-error :format-control "Fooey!")))
  1672. @t{ |> } Break: Fooey!
  1673. @t{ |> } BREAK entered because of *BREAK-ON-SIGNALS*.
  1674. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1675. @t{ |> } 1: Continue to signal.
  1676. @t{ |> } 2: Top level.
  1677. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 1}@b{<<|}
  1678. @t{ |> } Continue to signal.
  1679. @result{} NIL, #<SIMPLE-ERROR 32212257>
  1680. (let ((*break-on-signals* 'error))
  1681. (error 'simple-error :format-control "Fooey!"))
  1682. @t{ |> } Break: Fooey!
  1683. @t{ |> } BREAK entered because of *BREAK-ON-SIGNALS*.
  1684. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1685. @t{ |> } 1: Continue to signal.
  1686. @t{ |> } 2: Top level.
  1687. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 1}@b{<<|}
  1688. @t{ |> } Continue to signal.
  1689. @t{ |> } Error: Fooey!
  1690. @t{ |> } To continue, type :CONTINUE followed by an option number:
  1691. @t{ |> } 1: Top level.
  1692. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 1}@b{<<|}
  1693. @t{ |> } Top level.
  1694. @end example
  1695. @subsubheading See Also::
  1696. @ref{break}
  1697. ,
  1698. @ref{signal}
  1699. ,
  1700. @ref{warn}
  1701. ,
  1702. @ref{error}
  1703. ,
  1704. @ref{typep}
  1705. ,
  1706. @ref{Condition System Concepts}
  1707. @subsubheading Notes::
  1708. @b{*break-on-signals*} is intended primarily for use in debugging code that
  1709. does signaling. When setting @b{*break-on-signals*}, the user is
  1710. encouraged to choose the most restrictive specification that suffices.
  1711. Setting @b{*break-on-signals*} effectively violates the modular handling of
  1712. @i{condition} signaling. In practice, the complete effect of setting
  1713. @b{*break-on-signals*} might be unpredictable in some cases since the user
  1714. might not be aware of the variety or number of calls to @b{signal}
  1715. that are used in code called only incidentally.
  1716. @b{*break-on-signals*} enables an early entry to the debugger but such an
  1717. entry does not preclude an additional entry to the debugger in the case of
  1718. operations such as @b{error} and @b{cerror}.
  1719. @node handler-bind, handler-case, *break-on-signals*, Conditions Dictionary
  1720. @subsection handler-bind [Macro]
  1721. @code{handler-bind} @i{@r{(}@{!@i{binding}@}{*}@r{)}
  1722. @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  1723. @w{@i{binding} ::=@r{(}type handler@r{)}}
  1724. @subsubheading Arguments and Values::
  1725. @i{type}---a @i{type specifier}.
  1726. @i{handler}---a @i{form}; evaluated to produce a @i{handler-function}.
  1727. @i{handler-function}---a @i{designator} for a @i{function} of one @i{argument}.
  1728. @i{forms}---an @i{implicit progn}.
  1729. @i{results}---the @i{values} returned by the @i{forms}.
  1730. @subsubheading Description::
  1731. Executes @i{forms} in a @i{dynamic environment} where the indicated
  1732. @i{handler} @i{bindings} are in effect.
  1733. Each @i{handler} should evaluate to a @i{handler-function},
  1734. which is used to handle @i{conditions} of the given @i{type}
  1735. during execution of the @i{forms}. This @i{function} should
  1736. take a single argument, the @i{condition} being signaled.
  1737. If more than one @i{handler} @i{binding} is supplied,
  1738. the @i{handler} @i{bindings} are searched sequentially from
  1739. top to bottom in search of a match (by visual analogy with @b{typecase}).
  1740. If an appropriate @i{type} is found,
  1741. the associated handler is run in a @i{dynamic environment} where none of these
  1742. @i{handler} bindings are visible (to avoid recursive errors).
  1743. If the @i{handler} @i{declines}, the search continues for another @i{handler}.
  1744. If no appropriate @i{handler} is found, other @i{handlers} are sought
  1745. from dynamically enclosing contours. If no @i{handler} is found outside,
  1746. then @b{signal} returns or @b{error} enters the debugger.
  1747. @subsubheading Examples::
  1748. In the following code, if an unbound variable error is
  1749. signaled in the body (and not handled by an intervening handler),
  1750. the first function is called.
  1751. @example
  1752. (handler-bind ((unbound-variable #'(lambda ...))
  1753. (error #'(lambda ...)))
  1754. ...)
  1755. @end example
  1756. If any other kind of error is signaled, the second function is called.
  1757. In either case, neither handler is active while executing the code
  1758. in the associated function.
  1759. @example
  1760. (defun trap-error-handler (condition)
  1761. (format *error-output* "~&~A~&" condition)
  1762. (throw 'trap-errors nil))
  1763. (defmacro trap-errors (&rest forms)
  1764. `(catch 'trap-errors
  1765. (handler-bind ((error #'trap-error-handler))
  1766. ,@@forms)))
  1767. (list (trap-errors (signal "Foo.") 1)
  1768. (trap-errors (error "Bar.") 2)
  1769. (+ 1 2))
  1770. @t{ |> } Bar.
  1771. @result{} (1 NIL 3)
  1772. @end example
  1773. Note that ``Foo.'' is not printed because the condition made
  1774. by @b{signal} is a @i{simple condition}, which is not of @i{type} @b{error},
  1775. so it doesn't trigger the handler for @b{error} set up by @t{trap-errors}.
  1776. @subsubheading See Also::
  1777. @ref{handler-case}
  1778. @node handler-case, ignore-errors, handler-bind, Conditions Dictionary
  1779. @subsection handler-case [Macro]
  1780. @code{handler-case} @i{@i{expression}
  1781. [[@{!@i{error-clause}@}{*} | !@i{no-error-clause}]]} @result{} @i{@{@i{result}@}{*}}
  1782. @w{@i{clause} ::=!@i{error-clause} | !@i{no-error-clause}}
  1783. @w{@i{error-clause} ::=@r{(}typespec @r{(}@t{[}var@t{]}@r{)} @{@i{declaration}@}{*} @{@i{form}@}{*}@r{)}}
  1784. @w{@i{no-error-clause} ::=@r{(}@t{:no-error} @i{lambda-list} @{@i{declaration}@}{*} @{@i{form}@}{*}@r{)}}
  1785. @subsubheading Arguments and Values::
  1786. @i{expression}---a @i{form}.
  1787. @i{typespec}---a @i{type specifier}.
  1788. @i{var}---a @i{variable} @i{name}.
  1789. @i{lambda-list}---an @i{ordinary lambda list}.
  1790. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1791. @i{form}---a @i{form}.
  1792. @i{results}---In the normal situation, the values returned are those that result from
  1793. the evaluation of @i{expression};
  1794. in the exceptional situation when control is transferred to a @i{clause},
  1795. the value of the last @i{form} in that @i{clause} is returned.
  1796. @subsubheading Description::
  1797. @b{handler-case} executes @i{expression} in a @i{dynamic environment} where
  1798. various handlers are active. Each @i{error-clause} specifies how to
  1799. handle a @i{condition} matching the indicated @i{typespec}.
  1800. A @i{no-error-clause} allows the specification of a particular action
  1801. if control returns normally.
  1802. If a @i{condition} is signaled for which there is an appropriate
  1803. @i{error-clause} during the execution of @i{expression}
  1804. (@i{i.e.}, one for which @t{(typep @i{condition} '@i{typespec})}
  1805. returns @i{true}) and if there is no intervening handler for a
  1806. @i{condition} of that @i{type}, then control is transferred to
  1807. the body of the relevant @i{error-clause}. In this case, the
  1808. dynamic state is unwound appropriately (so that the handlers established
  1809. around the @i{expression} are no longer active), and @i{var} is bound to
  1810. the @i{condition} that had been signaled.
  1811. If more than one case is provided, those cases are made accessible
  1812. in parallel. That is, in
  1813. @example
  1814. (handler-case @i{form}
  1815. (@i{typespec1} (@i{var1}) @i{form1})
  1816. (@i{typespec2} (@i{var2}) @i{form2}))
  1817. @end example
  1818. if the first @i{clause} (containing @i{form1}) has been selected,
  1819. the handler for the second is no longer visible (or vice versa).
  1820. The @i{clauses}
  1821. are searched sequentially from top to bottom. If there is @i{type}
  1822. overlap between @i{typespecs},
  1823. the earlier of the @i{clauses} is selected.
  1824. If @i{var}
  1825. is not needed, it can be omitted. That is, a @i{clause} such as:
  1826. @example
  1827. (@i{typespec} (@i{var}) (declare (ignore @i{var})) @i{form})
  1828. @end example
  1829. can be written
  1830. @t{(@i{typespec} () @i{form})}.
  1831. If there are no @i{forms} in a selected @i{clause}, the case, and therefore
  1832. @b{handler-case}, returns @b{nil}.
  1833. If execution of @i{expression}
  1834. returns normally and no @i{no-error-clause}
  1835. exists, the values returned by
  1836. @i{expression} are returned by @b{handler-case}.
  1837. If execution of
  1838. @i{expression} returns normally and a @i{no-error-clause}
  1839. does exist, the values returned are used as arguments to the function
  1840. described by constructing
  1841. @t{(lambda @i{lambda-list} @{@i{form}@}{*})}
  1842. from the @i{no-error-clause}, and the @i{values} of that function call are
  1843. returned by @b{handler-case}.
  1844. The handlers which were established around the @i{expression} are no longer active at the time of this call.
  1845. @subsubheading Examples::
  1846. @example
  1847. (defun assess-condition (condition)
  1848. (handler-case (signal condition)
  1849. (warning () "Lots of smoke, but no fire.")
  1850. ((or arithmetic-error control-error cell-error stream-error)
  1851. (condition)
  1852. (format nil "~S looks especially bad." condition))
  1853. (serious-condition (condition)
  1854. (format nil "~S looks serious." condition))
  1855. (condition () "Hardly worth mentioning.")))
  1856. @result{} ASSESS-CONDITION
  1857. (assess-condition (make-condition 'stream-error :stream *terminal-io*))
  1858. @result{} "#<STREAM-ERROR 12352256> looks especially bad."
  1859. (define-condition random-condition (condition) ()
  1860. (:report (lambda (condition stream)
  1861. (declare (ignore condition))
  1862. (princ "Yow" stream))))
  1863. @result{} RANDOM-CONDITION
  1864. (assess-condition (make-condition 'random-condition))
  1865. @result{} "Hardly worth mentioning."
  1866. @end example
  1867. @subsubheading See Also::
  1868. @ref{handler-bind}
  1869. ,
  1870. @ref{ignore-errors}
  1871. ,
  1872. @ref{Condition System Concepts}
  1873. @subsubheading Notes::
  1874. @example
  1875. (handler-case form
  1876. (@i{type1} (@i{var1}) . @i{body1})
  1877. (@i{type2} (@i{var2}) . @i{body2}) ...)
  1878. @end example
  1879. is approximately equivalent to:
  1880. @example
  1881. (block #1=#:g0001
  1882. (let ((#2=#:g0002 nil))
  1883. (tagbody
  1884. (handler-bind ((@i{type1} #'(lambda (temp)
  1885. (setq #1# temp)
  1886. (go #3=#:g0003)))
  1887. (@i{type2} #'(lambda (temp)
  1888. (setq #2# temp)
  1889. (go #4=#:g0004))) ...)
  1890. (return-from #1# form))
  1891. #3# (return-from #1# (let ((@i{var1} #2#)) . @i{body1}))
  1892. #4# (return-from #1# (let ((@i{var2} #2#)) . @i{body2})) ...)))
  1893. @end example
  1894. @example
  1895. (handler-case form
  1896. (@i{type1} @i{(var1)} . @i{body1})
  1897. ...
  1898. (:no-error (@i{varN-1} @i{varN-2} ...) . @i{bodyN}))
  1899. @end example
  1900. is approximately equivalent to:
  1901. @example
  1902. (block #1=#:error-return
  1903. (multiple-value-call #'(lambda (@i{varN-1} @i{varN-2} ...) . @i{bodyN})
  1904. (block #2=#:normal-return
  1905. (return-from #1#
  1906. (handler-case (return-from #2# form)
  1907. (@i{type1} (@i{var1}) . @i{body1}) ...)))))
  1908. @end example
  1909. @node ignore-errors, define-condition, handler-case, Conditions Dictionary
  1910. @subsection ignore-errors [Macro]
  1911. @code{ignore-errors} @i{@{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  1912. @subsubheading Arguments and Values::
  1913. @i{forms}---an @i{implicit progn}.
  1914. @i{results}---In the normal situation,
  1915. the @i{values} of the @i{forms} are returned;
  1916. in the exceptional situation,
  1917. two values are returned: @b{nil} and the @i{condition}.
  1918. @subsubheading Description::
  1919. @b{ignore-errors} is used to prevent @i{conditions} of @i{type} @b{error}
  1920. from causing entry into the debugger.
  1921. Specifically, @b{ignore-errors} @i{executes} @i{forms}
  1922. in a @i{dynamic environment} where a @i{handler} for
  1923. @i{conditions} of @i{type} @b{error} has been established;
  1924. if invoked, it @i{handles} such @i{conditions} by
  1925. returning two @i{values}, @b{nil} and the @i{condition} that was @i{signaled},
  1926. from the @b{ignore-errors} @i{form}.
  1927. If a @i{normal return} from the @i{forms} occurs,
  1928. any @i{values} returned are returned by @b{ignore-errors}.
  1929. @subsubheading Examples::
  1930. @example
  1931. (defun load-init-file (program)
  1932. (let ((win nil))
  1933. (ignore-errors ;if this fails, don't enter debugger
  1934. (load (merge-pathnames (make-pathname :name program :type :lisp)
  1935. (user-homedir-pathname)))
  1936. (setq win t))
  1937. (unless win (format t "~&Init file failed to load.~
  1938. win))
  1939. (load-init-file "no-such-program")
  1940. @t{ |> } Init file failed to load.
  1941. NIL
  1942. @end example
  1943. @subsubheading See Also::
  1944. @ref{handler-case}
  1945. , @ref{Condition System Concepts}
  1946. @subsubheading Notes::
  1947. @example
  1948. (ignore-errors . @i{forms})
  1949. @end example
  1950. is equivalent to:
  1951. @example
  1952. (handler-case (progn . @i{forms})
  1953. (error (condition) (values nil condition)))
  1954. @end example
  1955. Because the second return value is a @i{condition}
  1956. in the exceptional case, it is common (but not required) to arrange
  1957. for the second return value in the normal case to be missing or @b{nil} so
  1958. that the two situations can be distinguished.
  1959. @node define-condition, make-condition, ignore-errors, Conditions Dictionary
  1960. @subsection define-condition [Macro]
  1961. [Editorial Note by KMP: This syntax stuff is still very confused and needs lots of work.]
  1962. @code{define-condition} @i{name @r{(}@{@i{parent-type}@}{*}@r{)}
  1963. @r{(}@{!@i{slot-spec}@}{*}@r{)}
  1964. @{@i{option}@}{*}}@*
  1965. @result{} @i{name}
  1966. @w{@i{slot-spec} ::=slot-name | @r{(}slot-name !@i{slot-option}@r{)}}
  1967. @w{@i{slot-option} ::=[[ @{{:reader} @i{symbol}@}{*} | }
  1968. @w{ @{{:writer} !@i{function-name}@}{*} | }
  1969. @w{ @{{:accessor} @i{symbol}@}{*} | }
  1970. @w{ @{{:allocation} !@i{allocation-type}@} | }
  1971. @w{ @{{:initarg} @i{symbol}@}{*} | }
  1972. @w{ @{{:initform} @i{form}@} | }
  1973. @w{ @{{:type} @i{type-specifier}@} ]]}
  1974. @w{@i{option} ::=[[ @r{(}@t{:default-initargs} @t{.} @i{initarg-list}@r{)} | }
  1975. @w{ @r{(}@t{:documentation} @i{string}@r{)} | }
  1976. @w{ @r{(}@t{:report} @i{report-name}@r{)} ]]}
  1977. @w{@i{function-name} ::=@{@i{symbol} | @t{(setf @i{symbol})}@}}
  1978. @w{@i{allocation-type} ::=@t{:instance} | @t{:class}}
  1979. @w{@i{report-name} ::=@i{string} | @i{symbol} | @i{lambda expression}}
  1980. @subsubheading Arguments and Values::
  1981. @i{name}---a @i{symbol}.
  1982. @i{parent-type}---a @i{symbol} naming a @i{condition} @i{type}.
  1983. If no @i{parent-types} are supplied,
  1984. the @i{parent-types} default to @t{(condition)}.
  1985. @i{default-initargs}---a @i{list} of @i{keyword/value pairs}.
  1986. [Editorial Note by KMP: This is all mixed up as to which is a slot option and which is
  1987. a main option. I'll sort that out. Also, some of this is implied
  1988. by the bnf and needn't be stated explicitly.]
  1989. @i{Slot-spec} -- the @i{name} of a @i{slot} or a @i{list}
  1990. consisting of the @i{slot-name} followed by zero or more @i{slot-options}.
  1991. @i{Slot-name} -- a slot name (a @i{symbol}),
  1992. the @i{list} of a slot name, or the
  1993. @i{list} of slot name/slot form pairs.
  1994. @i{Option} -- Any of the following:
  1995. @table @asis
  1996. @item @t{:reader}
  1997. @t{:reader} can be supplied more than once for a given @i{slot}
  1998. and cannot be @b{nil}.
  1999. @item @t{:writer}
  2000. @t{:writer} can be supplied more than once for a given @i{slot}
  2001. and must name a @i{generic function}.
  2002. @item @t{:accessor}
  2003. @t{:accessor} can be supplied more than once for a given @i{slot}
  2004. and cannot be @b{nil}.
  2005. @item @t{:allocation}
  2006. @t{:allocation} can be supplied once at most for a given @i{slot}.
  2007. The default if @t{:allocation} is not supplied is @t{:instance}.
  2008. @item @t{:initarg}
  2009. @t{:initarg} can be supplied more than once for a given @i{slot}.
  2010. @item @t{:initform}
  2011. @t{:initform} can be supplied once at most for a given @i{slot}.
  2012. @item @t{:type}
  2013. @t{:type} can be supplied once at most for a given @i{slot}.
  2014. @item @t{:documentation}
  2015. @t{:documentation} can be supplied once at most for a given @i{slot}.
  2016. @item @t{:report}
  2017. @t{:report} can be supplied once at most.
  2018. @end table
  2019. @subsubheading Description::
  2020. @b{define-condition} defines a new condition type called @i{name},
  2021. which is a @i{subtype} of
  2022. the @i{type} or @i{types} named by
  2023. @i{parent-type}.
  2024. Each @i{parent-type} argument specifies a direct @i{supertype}
  2025. of the new @i{condition}. The new @i{condition}
  2026. inherits @i{slots} and @i{methods} from each of its direct
  2027. @i{supertypes}, and so on.
  2028. If a slot name/slot form pair is supplied,
  2029. the slot form is a @i{form} that
  2030. can be evaluated by @b{make-condition} to
  2031. produce a default value when an explicit value is not provided. If no
  2032. slot form
  2033. is supplied, the contents of the @i{slot}
  2034. is initialized in an
  2035. @i{implementation-dependent} way.
  2036. If the @i{type} being defined and some other
  2037. @i{type} from which it inherits
  2038. have a slot by the same name, only one slot is allocated in the
  2039. @i{condition},
  2040. but the supplied slot form overrides any slot form
  2041. that might otherwise have been inherited from a @i{parent-type}. If no
  2042. slot form is supplied, the inherited slot form (if any) is still visible.
  2043. Accessors are created according to the same rules as used by
  2044. @b{defclass}.
  2045. A description of @i{slot-options} follows:
  2046. @table @asis
  2047. @item @t{:reader}
  2048. The @t{:reader} slot option specifies that an @i{unqualified method} is
  2049. to be defined on the @i{generic function} named by the argument
  2050. to @t{:reader} to read the value of the given @i{slot}.
  2051. @item @t{*}
  2052. The @t{:initform} slot option is used to provide a default
  2053. initial value form to be used in the initialization of the @i{slot}. This
  2054. @i{form} is evaluated every time it is used to initialize the
  2055. @i{slot}. The
  2056. @i{lexical environment}
  2057. in which this @i{form} is evaluated is the lexical
  2058. @i{environment} in which the @b{define-condition}
  2059. form was evaluated.
  2060. Note that the @i{lexical environment} refers both to variables and to
  2061. @i{functions}.
  2062. For @i{local slots}, the @i{dynamic environment} is the dynamic
  2063. @i{environment}
  2064. in which @b{make-condition} was called; for
  2065. @i{shared slots}, the @i{dynamic environment}
  2066. is the @i{dynamic environment} in which the
  2067. @b{define-condition} form was evaluated.
  2068. [Reviewer Note by Barmar: Issue CLOS-CONDITIONS doesn't say this.]
  2069. No implementation is permitted to extend the syntax of @b{define-condition}
  2070. to allow @t{(@i{slot-name} @i{form})} as an abbreviation for
  2071. @t{(@i{slot-name} :initform @i{form})}.
  2072. @item @t{:initarg}
  2073. The @t{:initarg} slot option declares an initialization
  2074. argument named by its @i{symbol} argument
  2075. and specifies that this
  2076. initialization argument initializes the given @i{slot}. If the
  2077. initialization argument has a value in the call to
  2078. @b{initialize-instance}, the value is stored into the given @i{slot},
  2079. and the slot's @t{:initform} slot option, if any, is not
  2080. evaluated. If none of the initialization arguments specified for a
  2081. given @i{slot} has a value, the @i{slot} is initialized according to the
  2082. @t{:initform} slot option, if specified.
  2083. @item @t{:type}
  2084. The @t{:type} slot option specifies that the contents of the
  2085. @i{slot} is always of the specified @i{type}. It effectively
  2086. declares the result type of the reader generic function when applied
  2087. to an @i{object} of this @i{condition} type.
  2088. The consequences of attempting to store in a
  2089. @i{slot} a value that
  2090. does not satisfy the type of the @i{slot} is undefined.
  2091. @item @t{:default-initargs}
  2092. [Editorial Note by KMP: This is an option, not a slot option.]
  2093. This option is treated the same as it would be @b{defclass}.
  2094. @item @t{:documentation}
  2095. [Editorial Note by KMP: This is both an option and a slot option.]
  2096. The @t{:documentation} slot option provides a @i{documentation string}
  2097. for the @i{slot}.
  2098. @item @t{:report}
  2099. [Editorial Note by KMP: This is an option, not a slot option.]
  2100. @i{Condition} reporting is mediated through the @b{print-object}
  2101. method for the @i{condition} type in question, with @b{*print-escape*}
  2102. always being @b{nil}. Specifying @t{(:report @i{report-name})}
  2103. in the definition of a condition type @t{C} is equivalent to:
  2104. @example
  2105. (defmethod print-object ((x c) stream)
  2106. (if *print-escape* (call-next-method) (@i{report-name} x stream)))
  2107. @end example
  2108. If the value supplied by the argument to @t{:report} (@i{report-name})
  2109. is a @i{symbol} or a @i{lambda expression},
  2110. it must be acceptable to
  2111. @b{function}. @t{(function @i{report-name})}
  2112. is evaluated
  2113. in the current @i{lexical environment}.
  2114. It should return a @i{function}
  2115. of two
  2116. arguments, a @i{condition} and a @i{stream},
  2117. that prints on the @i{stream} a
  2118. description of the @i{condition}.
  2119. This @i{function} is called whenever the
  2120. @i{condition} is printed while @b{*print-escape*} is @b{nil}.
  2121. If @i{report-name} is a @i{string}, it is a shorthand for
  2122. @example
  2123. (lambda (condition stream)
  2124. (declare (ignore condition))
  2125. (write-string @i{report-name} stream))
  2126. @end example
  2127. This option is processed after the new @i{condition} type has been defined,
  2128. so use of the @i{slot} accessors within the @t{:report} function is permitted.
  2129. If this option is not supplied, information about how to report this
  2130. type of @i{condition} is inherited from the @i{parent-type}.
  2131. @end table
  2132. The consequences are unspecifed if an attempt is made to @i{read} a
  2133. @i{slot} that has not been explicitly initialized and that has not
  2134. been given a default value.
  2135. The consequences are unspecified if an attempt is made to assign the
  2136. @i{slots} by using @b{setf}.
  2137. If a @b{define-condition} @i{form} appears as a @i{top level form},
  2138. the @i{compiler} must make @i{name} recognizable as a valid @i{type} name,
  2139. and it must be possible to reference the @i{condition} @i{type} as the
  2140. @i{parent-type} of another @i{condition} @i{type} in a subsequent
  2141. @b{define-condition} @i{form} in the @i{file} being compiled.
  2142. @subsubheading Examples::
  2143. The following form defines a condition of @i{type}
  2144. @t{peg/hole-mismatch} which inherits from a condition type
  2145. called @t{blocks-world-error}:
  2146. @example
  2147. (define-condition peg/hole-mismatch
  2148. (blocks-world-error)
  2149. ((peg-shape :initarg :peg-shape
  2150. :reader peg/hole-mismatch-peg-shape)
  2151. (hole-shape :initarg :hole-shape
  2152. :reader peg/hole-mismatch-hole-shape))
  2153. (:report (lambda (condition stream)
  2154. (format stream "A ~A peg cannot go in a ~A hole."
  2155. (peg/hole-mismatch-peg-shape condition)
  2156. (peg/hole-mismatch-hole-shape condition)))))
  2157. @end example
  2158. The new type has slots @t{peg-shape} and @t{hole-shape},
  2159. so @b{make-condition} accepts @t{:peg-shape} and @t{:hole-shape} keywords.
  2160. The @i{readers} @t{peg/hole-mismatch-peg-shape} and @t{peg/hole-mismatch-hole-shape}
  2161. apply to objects of this type, as illustrated in the @t{:report} information.
  2162. The following form defines a @i{condition} @i{type} named @t{machine-error}
  2163. which inherits from @b{error}:
  2164. @example
  2165. (define-condition machine-error
  2166. (error)
  2167. ((machine-name :initarg :machine-name
  2168. :reader machine-error-machine-name))
  2169. (:report (lambda (condition stream)
  2170. (format stream "There is a problem with ~A."
  2171. (machine-error-machine-name condition)))))
  2172. @end example
  2173. Building on this definition, a new error condition can be defined which
  2174. is a subtype of @t{machine-error} for use when machines are not available:
  2175. @example
  2176. (define-condition machine-not-available-error (machine-error) ()
  2177. (:report (lambda (condition stream)
  2178. (format stream "The machine ~A is not available."
  2179. (machine-error-machine-name condition)))))
  2180. @end example
  2181. This defines a still more specific condition, built upon
  2182. @t{machine-not-available-error}, which provides a slot initialization form
  2183. for @t{machine-name} but which does not provide any new slots or report
  2184. information. It just gives the @t{machine-name} slot a default initialization:
  2185. @example
  2186. (define-condition my-favorite-machine-not-available-error
  2187. (machine-not-available-error)
  2188. ((machine-name :initform "mc.lcs.mit.edu")))
  2189. @end example
  2190. Note that since no @t{:report} clause was given, the information
  2191. inherited from @t{machine-not-available-error} is used to
  2192. report this type of condition.
  2193. @example
  2194. (define-condition ate-too-much (error)
  2195. ((person :initarg :person :reader ate-too-much-person)
  2196. (weight :initarg :weight :reader ate-too-much-weight)
  2197. (kind-of-food :initarg :kind-of-food
  2198. :reader :ate-too-much-kind-of-food)))
  2199. @result{} ATE-TOO-MUCH
  2200. (define-condition ate-too-much-ice-cream (ate-too-much)
  2201. ((kind-of-food :initform 'ice-cream)
  2202. (flavor :initarg :flavor
  2203. :reader ate-too-much-ice-cream-flavor
  2204. :initform 'vanilla ))
  2205. (:report (lambda (condition stream)
  2206. (format stream "~A ate too much ~A ice-cream"
  2207. (ate-too-much-person condition)
  2208. (ate-too-much-ice-cream-flavor condition)))))
  2209. @result{} ATE-TOO-MUCH-ICE-CREAM
  2210. (make-condition 'ate-too-much-ice-cream
  2211. :person 'fred
  2212. :weight 300
  2213. :flavor 'chocolate)
  2214. @result{} #<ATE-TOO-MUCH-ICE-CREAM 32236101>
  2215. (format t "~A" *)
  2216. @t{ |> } FRED ate too much CHOCOLATE ice-cream
  2217. @result{} NIL
  2218. @end example
  2219. @subsubheading See Also::
  2220. @ref{make-condition}
  2221. ,
  2222. @ref{defclass}
  2223. , @ref{Condition System Concepts}
  2224. @node make-condition, restart, define-condition, Conditions Dictionary
  2225. @subsection make-condition [Function]
  2226. @code{make-condition} @i{type {&rest} slot-initializations} @result{} @i{condition}
  2227. @subsubheading Arguments and Values::
  2228. @i{type}---a @i{type specifier} (for a @i{subtype} of @b{condition}).
  2229. @i{slot-initializations}---an @i{initialization argument list}.
  2230. @i{condition}---a @i{condition}.
  2231. @subsubheading Description::
  2232. Constructs and returns a @i{condition} of type @i{type}
  2233. using @i{slot-initializations} for the initial values of the slots.
  2234. The newly created @i{condition} is returned.
  2235. @subsubheading Examples::
  2236. @example
  2237. (defvar *oops-count* 0)
  2238. (setq a (make-condition 'simple-error
  2239. :format-control "This is your ~:R error."
  2240. :format-arguments (list (incf *oops-count*))))
  2241. @result{} #<SIMPLE-ERROR 32245104>
  2242. (format t "~&~A~
  2243. @t{ |> } This is your first error.
  2244. @result{} NIL
  2245. (error a)
  2246. @t{ |> } Error: This is your first error.
  2247. @t{ |> } To continue, type :CONTINUE followed by an option number:
  2248. @t{ |> } 1: Return to Lisp Toplevel.
  2249. @t{ |> } Debug>
  2250. @end example
  2251. @subsubheading Affected By::
  2252. The set of defined @i{condition} @i{types}.
  2253. @subsubheading See Also::
  2254. @ref{define-condition}
  2255. , @ref{Condition System Concepts}
  2256. @node restart, compute-restarts, make-condition, Conditions Dictionary
  2257. @subsection restart [System Class]
  2258. @subsubheading Class Precedence List::
  2259. @b{restart},
  2260. @b{t}
  2261. @subsubheading Description::
  2262. An @i{object} of @i{type} @b{restart} represents a @i{function} that can be
  2263. called to perform some form of recovery action, usually a transfer of control
  2264. to an outer point in the running program.
  2265. An @i{implementation} is free to implement a @i{restart} in whatever
  2266. manner is most convenient; a @i{restart} has only @i{dynamic extent}
  2267. relative to the scope of the binding @i{form} which @i{establishes} it.
  2268. @node compute-restarts, find-restart, restart, Conditions Dictionary
  2269. @subsection compute-restarts [Function]
  2270. @code{compute-restarts} @i{{&optional} condition} @result{} @i{restarts}
  2271. @subsubheading Arguments and Values::
  2272. @i{condition}---a @i{condition} @i{object}, or @b{nil}.
  2273. @i{restarts}---a @i{list} of @i{restarts}.
  2274. @subsubheading Description::
  2275. @b{compute-restarts} uses the dynamic state of the program to compute
  2276. a @i{list} of the @i{restarts} which are currently active.
  2277. The resulting @i{list} is ordered so that the innermost
  2278. (more-recently established) restarts are nearer the head of the @i{list}.
  2279. When @i{condition} is @i{non-nil}, only those @i{restarts}
  2280. are considered that are either explicitly associated with that @i{condition},
  2281. or not associated with any @i{condition}; that is, the excluded @i{restarts}
  2282. are those that are associated with a non-empty set of @i{conditions} of
  2283. which the given @i{condition} is not an @i{element}.
  2284. If @i{condition} is @b{nil}, all @i{restarts} are considered.
  2285. @b{compute-restarts} returns all
  2286. @i{applicable restarts},
  2287. including anonymous ones, even if some of them have the same name as
  2288. others and would therefore not be found by @b{find-restart}
  2289. when given a @i{symbol} argument.
  2290. Implementations are permitted, but not required, to return @i{distinct}
  2291. @i{lists} from repeated calls to @b{compute-restarts} while in
  2292. the same dynamic environment.
  2293. The consequences are undefined if the @i{list} returned by
  2294. @b{compute-restarts} is every modified.
  2295. @subsubheading Examples::
  2296. @example
  2297. ;; One possible way in which an interactive debugger might present
  2298. ;; restarts to the user.
  2299. (defun invoke-a-restart ()
  2300. (let ((restarts (compute-restarts)))
  2301. (do ((i 0 (+ i 1)) (r restarts (cdr r))) ((null r))
  2302. (format t "~&~D: ~A~
  2303. (let ((n nil) (k (length restarts)))
  2304. (loop (when (and (typep n 'integer) (>= n 0) (< n k))
  2305. (return t))
  2306. (format t "~&Option: ")
  2307. (setq n (read))
  2308. (fresh-line))
  2309. (invoke-restart-interactively (nth n restarts)))))
  2310. (restart-case (invoke-a-restart)
  2311. (one () 1)
  2312. (two () 2)
  2313. (nil () :report "Who knows?" 'anonymous)
  2314. (one () 'I)
  2315. (two () 'II))
  2316. @t{ |> } 0: ONE
  2317. @t{ |> } 1: TWO
  2318. @t{ |> } 2: Who knows?
  2319. @t{ |> } 3: ONE
  2320. @t{ |> } 4: TWO
  2321. @t{ |> } 5: Return to Lisp Toplevel.
  2322. @t{ |> } Option: @b{|>>}@t{4}@b{<<|}
  2323. @result{} II
  2324. ;; Note that in addition to user-defined restart points, COMPUTE-RESTARTS
  2325. ;; also returns information about any system-supplied restarts, such as
  2326. ;; the "Return to Lisp Toplevel" restart offered above.
  2327. @end example
  2328. @subsubheading Affected By::
  2329. Existing restarts.
  2330. @subsubheading See Also::
  2331. @ref{find-restart}
  2332. ,
  2333. @ref{invoke-restart}
  2334. ,
  2335. @ref{restart-bind}
  2336. @node find-restart, invoke-restart, compute-restarts, Conditions Dictionary
  2337. @subsection find-restart [Function]
  2338. @code{find-restart} @i{identifier {&optional} condition}
  2339. {restart}
  2340. @subsubheading Arguments and Values::
  2341. @i{identifier}---a @i{non-nil} @i{symbol}, or a @i{restart}.
  2342. @i{condition}---a @i{condition} @i{object}, or @b{nil}.
  2343. @i{restart}---a @i{restart} or @b{nil}.
  2344. @subsubheading Description::
  2345. @b{find-restart} searches for a particular @i{restart} in the
  2346. current @i{dynamic environment}.
  2347. When @i{condition} is @i{non-nil}, only those @i{restarts}
  2348. are considered that are either explicitly associated with that @i{condition},
  2349. or not associated with any @i{condition}; that is, the excluded @i{restarts}
  2350. are those that are associated with a non-empty set of @i{conditions} of
  2351. which the given @i{condition} is not an @i{element}.
  2352. If @i{condition} is @b{nil}, all @i{restarts} are considered.
  2353. If @i{identifier} is a @i{symbol}, then the innermost
  2354. (most recently established) @i{applicable restart} with that @i{name} is returned.
  2355. @b{nil} is returned if no such restart is found.
  2356. If @i{identifier} is a currently active restart, then it is returned.
  2357. Otherwise, @b{nil} is returned.
  2358. @subsubheading Examples::
  2359. @example
  2360. (restart-case
  2361. (let ((r (find-restart 'my-restart)))
  2362. (format t "~S is named ~S" r (restart-name r)))
  2363. (my-restart () nil))
  2364. @t{ |> } #<RESTART 32307325> is named MY-RESTART
  2365. @result{} NIL
  2366. (find-restart 'my-restart)
  2367. @result{} NIL
  2368. @end example
  2369. @subsubheading Affected By::
  2370. Existing restarts.
  2371. @b{restart-case}, @b{restart-bind}, @b{with-condition-restarts}.
  2372. @subsubheading See Also::
  2373. @ref{compute-restarts}
  2374. @subsubheading Notes::
  2375. @example
  2376. (find-restart @i{identifier})
  2377. @equiv{} (find @i{identifier} (compute-restarts) :key :restart-name)
  2378. @end example
  2379. Although anonymous restarts have a name of @b{nil},
  2380. the consequences are unspecified if @b{nil} is given as an @i{identifier}.
  2381. Occasionally, programmers lament that @b{nil} is not permissible as an
  2382. @i{identifier} argument. In most such cases, @b{compute-restarts}
  2383. can probably be used to simulate the desired effect.
  2384. @node invoke-restart, invoke-restart-interactively, find-restart, Conditions Dictionary
  2385. @subsection invoke-restart [Function]
  2386. @code{invoke-restart} @i{restart {&rest} arguments} @result{} @i{@{@i{result}@}{*}}
  2387. @subsubheading Arguments and Values::
  2388. @i{restart}---a @i{restart designator}.
  2389. @i{argument}---an @i{object}.
  2390. @i{results}---the @i{values} returned by the @i{function}
  2391. associated with @i{restart}, if that @i{function} returns.
  2392. @subsubheading Description::
  2393. Calls the @i{function} associated with @i{restart},
  2394. passing @i{arguments} to it.
  2395. @i{Restart} must be valid in the current @i{dynamic environment}.
  2396. @subsubheading Examples::
  2397. @example
  2398. (defun add3 (x) (check-type x number) (+ x 3))
  2399. (foo 'seven)
  2400. @t{ |> } Error: The value SEVEN was not of type NUMBER.
  2401. @t{ |> } To continue, type :CONTINUE followed by an option number:
  2402. @t{ |> } 1: Specify a different value to use.
  2403. @t{ |> } 2: Return to Lisp Toplevel.
  2404. @t{ |> } Debug> @b{|>>}@t{(invoke-restart 'store-value 7)}@b{<<|}
  2405. @result{} 10
  2406. @end example
  2407. @subsubheading Side Effects::
  2408. A non-local transfer of control might be done by the restart.
  2409. @subsubheading Affected By::
  2410. Existing restarts.
  2411. @subsubheading Exceptional Situations::
  2412. If @i{restart} is not valid, an error of @i{type} @b{control-error} is signaled.
  2413. @subsubheading See Also::
  2414. @ref{find-restart}
  2415. ,
  2416. @ref{restart-bind}
  2417. ,
  2418. @ref{restart-case}
  2419. ,
  2420. @ref{invoke-restart-interactively}
  2421. @subsubheading Notes::
  2422. The most common use for @b{invoke-restart} is in a @i{handler}.
  2423. It might be used explicitly, or implicitly through @b{invoke-restart-interactively}
  2424. or a @i{restart function}.
  2425. @i{Restart functions} call @b{invoke-restart}, not vice versa. That is,
  2426. @i{invoke-restart} provides primitive functionality, and @i{restart functions}
  2427. are non-essential ``syntactic sugar.''
  2428. @node invoke-restart-interactively, restart-bind, invoke-restart, Conditions Dictionary
  2429. @subsection invoke-restart-interactively [Function]
  2430. @code{invoke-restart-interactively} @i{restart} @result{} @i{@{@i{result}@}{*}}
  2431. @subsubheading Arguments and Values::
  2432. @i{restart}---a @i{restart designator}.
  2433. @i{results}---the @i{values} returned by the @i{function}
  2434. associated with @i{restart}, if that @i{function} returns.
  2435. @subsubheading Description::
  2436. @b{invoke-restart-interactively} calls the @i{function} associated
  2437. with @i{restart}, prompting for any necessary arguments.
  2438. If @i{restart} is a name, it must be valid in the current @i{dynamic environment}.
  2439. @b{invoke-restart-interactively}
  2440. prompts for arguments by executing
  2441. the code provided in the @t{:interactive} keyword to
  2442. @b{restart-case} or
  2443. @t{:interactive-function} keyword to @b{restart-bind}.
  2444. If no such options have been supplied in the corresponding
  2445. @b{restart-bind} or @b{restart-case},
  2446. then the consequences are undefined if the @i{restart} takes
  2447. required arguments. If the arguments are optional, an argument list of
  2448. @b{nil} is used.
  2449. Once the arguments have been determined,
  2450. @b{invoke-restart-interactively}
  2451. executes the following:
  2452. @example
  2453. (apply #'invoke-restart @i{restart} @i{arguments})
  2454. @end example
  2455. @subsubheading Examples::
  2456. @example
  2457. (defun add3 (x) (check-type x number) (+ x 3))
  2458. (add3 'seven)
  2459. @t{ |> } Error: The value SEVEN was not of type NUMBER.
  2460. @t{ |> } To continue, type :CONTINUE followed by an option number:
  2461. @t{ |> } 1: Specify a different value to use.
  2462. @t{ |> } 2: Return to Lisp Toplevel.
  2463. @t{ |> } Debug> @b{|>>}@t{(invoke-restart-interactively 'store-value)}@b{<<|}
  2464. @t{ |> } Type a form to evaluate and use: @b{|>>}@t{7}@b{<<|}
  2465. @result{} 10
  2466. @end example
  2467. @subsubheading Side Effects::
  2468. If prompting for arguments is necesary,
  2469. some typeout may occur (on @i{query I/O}).
  2470. A non-local transfer of control might be done by the restart.
  2471. @subsubheading Affected By::
  2472. @b{*query-io*}, active @i{restarts}
  2473. @subsubheading Exceptional Situations::
  2474. If @i{restart} is not valid, an error of @i{type} @b{control-error}
  2475. is signaled.
  2476. @subsubheading See Also::
  2477. @ref{find-restart}
  2478. ,
  2479. @ref{invoke-restart}
  2480. ,
  2481. @ref{restart-case}
  2482. ,
  2483. @ref{restart-bind}
  2484. @subsubheading Notes::
  2485. @b{invoke-restart-interactively} is used internally by the debugger
  2486. and may also be useful in implementing other portable, interactive debugging
  2487. tools.
  2488. @node restart-bind, restart-case, invoke-restart-interactively, Conditions Dictionary
  2489. @subsection restart-bind [Macro]
  2490. @code{restart-bind} @i{@r{(}@{{(}name function
  2491. @{!@i{key-val-pair}@}{*}@r{)}@}{)}
  2492. @{@i{form}@}{*}}@*
  2493. @result{} @i{@{@i{result}@}{*}}
  2494. @w{@i{key-val-pair} ::=@t{:interactive-function} {interactive-function} | }
  2495. @w{ @t{:report-function} {report-function} | }
  2496. @w{ @t{:test-function} {test-function}}
  2497. @subsubheading Arguments and Values::
  2498. @i{name}---a @i{symbol}; not evaluated.
  2499. @i{function}---a @i{form}; evaluated.
  2500. @i{forms}---an @i{implicit progn}.
  2501. @i{interactive-function}---a @i{form}; evaluated.
  2502. @i{report-function}---a @i{form}; evaluated.
  2503. @i{test-function}---a @i{form}; evaluated.
  2504. @i{results}---the @i{values} returned by the @i{forms}.
  2505. @subsubheading Description::
  2506. @b{restart-bind} executes the body of @i{forms}
  2507. in a @i{dynamic environment} where @i{restarts} with the given @i{names} are in effect.
  2508. If a @i{name} is @b{nil}, it indicates an anonymous restart;
  2509. if a @i{name} is a @i{non-nil} @i{symbol}, it indicates a named restart.
  2510. The @i{function}, @i{interactive-function}, and @i{report-function}
  2511. are unconditionally evaluated in the current lexical and dynamic environment
  2512. prior to evaluation of the body. Each of these @i{forms} must evaluate to
  2513. a @i{function}.
  2514. If @b{invoke-restart} is done on that restart,
  2515. the @i{function} which resulted from evaluating @i{function}
  2516. is called, in the @i{dynamic environment} of the @b{invoke-restart},
  2517. with the @i{arguments} given to @b{invoke-restart}.
  2518. The @i{function} may either perform a non-local transfer of control or may return normally.
  2519. If the restart is invoked interactively from the debugger
  2520. (using @b{invoke-restart-interactively}),
  2521. the arguments are defaulted by calling the @i{function}
  2522. which resulted from evaluating @i{interactive-function}.
  2523. That @i{function} may optionally prompt interactively on @i{query I/O},
  2524. and should return a @i{list} of arguments to be used by
  2525. @b{invoke-restart-interactively} when invoking the restart.
  2526. If a restart is invoked interactively but no @i{interactive-function} is used,
  2527. then an argument list of @b{nil} is used. In that case, the @i{function}
  2528. must be compatible with an empty argument list.
  2529. If the restart is presented interactively (@i{e.g.}, by the debugger),
  2530. the presentation is done by calling the @i{function} which resulted
  2531. from evaluating @i{report-function}.
  2532. This @i{function} must be a @i{function} of one argument, a @i{stream}.
  2533. It is expected to print a description of the action that the restart takes
  2534. to that @i{stream}.
  2535. This @i{function} is called any time the restart is printed
  2536. while @b{*print-escape*} is @b{nil}.
  2537. In the case of interactive invocation,
  2538. the result is dependent on the value of @t{:interactive-function}
  2539. as follows.
  2540. @table @asis
  2541. @item @t{:interactive-function}
  2542. @i{Value} is evaluated in the current lexical environment and
  2543. should return a @i{function} of no arguments which constructs a
  2544. @i{list} of arguments to be used by @b{invoke-restart-interactively}
  2545. when invoking this restart. The @i{function} may prompt interactively
  2546. using @i{query I/O} if necessary.
  2547. @item @t{:report-function}
  2548. @i{Value} is evaluated in the current lexical environment and
  2549. should return a @i{function} of one argument, a @i{stream}, which
  2550. prints on the @i{stream} a summary of the action that this restart
  2551. takes. This @i{function} is called whenever the restart is
  2552. reported (printed while @b{*print-escape*} is @b{nil}).
  2553. If no @t{:report-function} option is provided, the manner in which the
  2554. @i{restart} is reported is @i{implementation-dependent}.
  2555. @item @t{:test-function}
  2556. @i{Value} is evaluated in the current lexical environment and
  2557. should return a @i{function} of one argument, a @i{condition}, which
  2558. returns @i{true} if the restart is to be considered visible.
  2559. @end table
  2560. @subsubheading Affected By::
  2561. @b{*query-io*}.
  2562. @subsubheading See Also::
  2563. @ref{restart-case}
  2564. ,
  2565. @ref{with-simple-restart}
  2566. @subsubheading Notes::
  2567. @b{restart-bind} is primarily intended to be used to implement
  2568. @b{restart-case} and might be useful in implementing other
  2569. macros. Programmers who are uncertain about whether to use @b{restart-case}
  2570. or @b{restart-bind} should prefer @b{restart-case} for the cases where
  2571. it is powerful enough, using @b{restart-bind} only in cases where its full
  2572. generality is really needed.
  2573. @node restart-case, restart-name, restart-bind, Conditions Dictionary
  2574. @subsection restart-case [Macro]
  2575. @code{restart-case} @i{restartable-form {@{!@i{clause}@}}} @result{} @i{@{@i{result}@}{*}}
  2576. @w{@i{clause} ::=@r{(} case-name lambda-list }
  2577. @w{ [[@t{:interactive} interactive-expression | @t{:report} report-expression | @t{:test} test-expression]] }
  2578. @w{ @{@i{declaration}@}{*} @{@i{form}@}{*}@r{)}}
  2579. @subsubheading Arguments and Values::
  2580. @i{restartable-form}---a @i{form}.
  2581. @i{case-name}---a @i{symbol} or @b{nil}.
  2582. @i{lambda-list}---an @i{ordinary lambda list}.
  2583. @i{interactive-expression}---a @i{symbol} or a @i{lambda expression}.
  2584. @i{report-expression}---a @i{string},
  2585. a @i{symbol},
  2586. or a @i{lambda expression}.
  2587. @i{test-expression}---a @i{symbol} or a @i{lambda expression}.
  2588. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  2589. @i{form}---a @i{form}.
  2590. @i{results}---the @i{values} resulting from the @i{evaluation}
  2591. of @i{restartable-form},
  2592. or the @i{values} returned by the last @i{form}
  2593. executed in a chosen @i{clause},
  2594. or @b{nil}.
  2595. @subsubheading Description::
  2596. @b{restart-case} evaluates @i{restartable-form} in a @i{dynamic environment}
  2597. where the clauses have special meanings as points to which control may be transferred.
  2598. If @i{restartable-form} finishes executing and returns any values,
  2599. all values returned are returned by @b{restart-case} and
  2600. processing has completed. While @i{restartable-form} is executing, any code may
  2601. transfer control to one of the clauses (see @b{invoke-restart}).
  2602. If a transfer
  2603. occurs, the forms in the body of that clause is evaluated and any values
  2604. returned by the last such form are returned by
  2605. @b{restart-case}.
  2606. In this case, the
  2607. dynamic state is unwound appropriately (so that the restarts established
  2608. around the @i{restartable-form} are no longer active) prior to execution of the
  2609. clause.
  2610. If there are no @i{forms}
  2611. in a selected clause, @b{restart-case} returns @b{nil}.
  2612. If @i{case-name} is a @i{symbol}, it names this restart.
  2613. It is possible to have more than one clause use the same @i{case-name}.
  2614. In this case, the first clause with that name is found by @b{find-restart}.
  2615. The other clauses are accessible using @b{compute-restarts}.
  2616. Each @i{arglist} is an @i{ordinary lambda list} to be bound during the
  2617. execution of its corresponding @i{forms}. These parameters are used
  2618. by the @b{restart-case} clause to receive any necessary data from a call
  2619. to @b{invoke-restart}.
  2620. By default, @b{invoke-restart-interactively} passes no arguments and
  2621. all arguments must be optional in order to accomodate interactive
  2622. restarting. However, the arguments need not be optional if the
  2623. @t{:interactive}
  2624. keyword has been used to inform @b{invoke-restart-interactively}
  2625. about how to compute a proper argument list.
  2626. @i{Keyword} options have the following meaning.
  2627. @table @asis
  2628. @item @t{:interactive}
  2629. The @i{value} supplied by @t{:interactive @i{value}}
  2630. must be a suitable argument to @b{function}.
  2631. @t{(function @i{value})} is evaluated in the current lexical
  2632. environment. It should return a @i{function} of no arguments which
  2633. returns arguments to be used by
  2634. @b{invoke-restart-interactively} when it is invoked.
  2635. @b{invoke-restart-interactively}
  2636. is called in the dynamic
  2637. environment available prior to any restart attempt, and uses
  2638. @i{query I/O} for user interaction.
  2639. If a restart is invoked interactively but no @t{:interactive} option
  2640. was supplied, the argument list used in the invocation is the empty
  2641. list.
  2642. @item @t{:report}
  2643. If the @i{value} supplied by @t{:report @i{value}}
  2644. is a @i{lambda expression} or a @i{symbol}, it
  2645. must be acceptable to @b{function}.
  2646. @t{(function @i{value})} is evaluated in the current lexical
  2647. environment. It should return a @i{function} of one
  2648. argument, a @i{stream}, which prints on the @i{stream} a
  2649. description of the restart. This @i{function} is called
  2650. whenever the restart is printed while @b{*print-escape*} is @b{nil}.
  2651. If @i{value} is a @i{string}, it is a shorthand for
  2652. @example
  2653. (lambda (stream) (write-string value stream))
  2654. @end example
  2655. If a named restart is asked to report but no report information has been
  2656. supplied, the name of the restart is used in generating default report text.
  2657. When @b{*print-escape*} is @b{nil}, the
  2658. printer uses the report information for
  2659. a restart. For example, a debugger might announce the action of typing
  2660. a ``continue'' command by:
  2661. @example
  2662. (format t "~&~S -- ~A~
  2663. @end example
  2664. which might then display as something like:
  2665. @example
  2666. :CONTINUE -- Return to command level
  2667. @end example
  2668. The consequences are unspecified if an unnamed restart is specified
  2669. but no @t{:report} option is provided.
  2670. @item @t{:test}
  2671. The @i{value} supplied by @t{:test @i{value}}
  2672. must be a suitable argument to @b{function}.
  2673. @t{(function @i{value})} is evaluated in the current lexical
  2674. environment. It should return a @i{function} of one @i{argument}, the
  2675. @i{condition}, that
  2676. returns @i{true} if the restart is to be considered visible.
  2677. The default for this option is equivalent to @t{(lambda (c) (declare (ignore c)) t)}.
  2678. @end table
  2679. If the @i{restartable-form} is a @i{list} whose @i{car} is any of
  2680. the @i{symbols} @b{signal}, @b{error}, @b{cerror},
  2681. or @b{warn} (or is a @i{macro form} which macroexpands into such a
  2682. @i{list}), then @b{with-condition-restarts} is used implicitly
  2683. to associate the indicated @i{restarts} with the @i{condition} to be
  2684. signaled.
  2685. @subsubheading Examples::
  2686. @example
  2687. (restart-case
  2688. (handler-bind ((error #'(lambda (c)
  2689. (declare (ignore condition))
  2690. (invoke-restart 'my-restart 7))))
  2691. (error "Foo."))
  2692. (my-restart (&optional v) v))
  2693. @result{} 7
  2694. (define-condition food-error (error) ())
  2695. @result{} FOOD-ERROR
  2696. (define-condition bad-tasting-sundae (food-error)
  2697. ((ice-cream :initarg :ice-cream :reader bad-tasting-sundae-ice-cream)
  2698. (sauce :initarg :sauce :reader bad-tasting-sundae-sauce)
  2699. (topping :initarg :topping :reader bad-tasting-sundae-topping))
  2700. (:report (lambda (condition stream)
  2701. (format stream "Bad tasting sundae with ~S, ~S, and ~S"
  2702. (bad-tasting-sundae-ice-cream condition)
  2703. (bad-tasting-sundae-sauce condition)
  2704. (bad-tasting-sundae-topping condition)))))
  2705. @result{} BAD-TASTING-SUNDAE
  2706. (defun all-start-with-same-letter (symbol1 symbol2 symbol3)
  2707. (let ((first-letter (char (symbol-name symbol1) 0)))
  2708. (and (eql first-letter (char (symbol-name symbol2) 0))
  2709. (eql first-letter (char (symbol-name symbol3) 0)))))
  2710. @result{} ALL-START-WITH-SAME-LETTER
  2711. (defun read-new-value ()
  2712. (format t "Enter a new value: ")
  2713. (multiple-value-list (eval (read))))
  2714. @result{} READ-NEW-VALUE@page
  2715. (defun verify-or-fix-perfect-sundae (ice-cream sauce topping)
  2716. (do ()
  2717. ((all-start-with-same-letter ice-cream sauce topping))
  2718. (restart-case
  2719. (error 'bad-tasting-sundae
  2720. :ice-cream ice-cream
  2721. :sauce sauce
  2722. :topping topping)
  2723. (use-new-ice-cream (new-ice-cream)
  2724. :report "Use a new ice cream."
  2725. :interactive read-new-value
  2726. (setq ice-cream new-ice-cream))
  2727. (use-new-sauce (new-sauce)
  2728. :report "Use a new sauce."
  2729. :interactive read-new-value
  2730. (setq sauce new-sauce))
  2731. (use-new-topping (new-topping)
  2732. :report "Use a new topping."
  2733. :interactive read-new-value
  2734. (setq topping new-topping))))
  2735. (values ice-cream sauce topping))
  2736. @result{} VERIFY-OR-FIX-PERFECT-SUNDAE
  2737. (verify-or-fix-perfect-sundae 'vanilla 'caramel 'cherry)
  2738. @t{ |> } Error: Bad tasting sundae with VANILLA, CARAMEL, and CHERRY.
  2739. @t{ |> } To continue, type :CONTINUE followed by an option number:
  2740. @t{ |> } 1: Use a new ice cream.
  2741. @t{ |> } 2: Use a new sauce.
  2742. @t{ |> } 3: Use a new topping.
  2743. @t{ |> } 4: Return to Lisp Toplevel.
  2744. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  2745. @t{ |> } Use a new ice cream.
  2746. @t{ |> } Enter a new ice cream: @b{|>>}@t{'chocolate}@b{<<|}
  2747. @result{} CHOCOLATE, CARAMEL, CHERRY
  2748. @end example
  2749. @subsubheading See Also::
  2750. @ref{restart-bind}
  2751. ,
  2752. @ref{with-simple-restart}
  2753. .
  2754. @subsubheading Notes::
  2755. @example
  2756. (restart-case @i{expression}
  2757. (@i{name1} @i{arglist1} ...@i{options1}... . @i{body1})
  2758. (@i{name2} @i{arglist2} ...@i{options2}... . @i{body2}))
  2759. @end example
  2760. is essentially equivalent to
  2761. @example
  2762. (block #1=#:g0001
  2763. (let ((#2=#:g0002 nil))
  2764. (tagbody
  2765. (restart-bind ((name1 #'(lambda (&rest temp)
  2766. (setq #2# temp)
  2767. (go #3=#:g0003))
  2768. ...@i{slightly-transformed-options1}...)
  2769. (name2 #'(lambda (&rest temp)
  2770. (setq #2# temp)
  2771. (go #4=#:g0004))
  2772. ...@i{slightly-transformed-options2}...))
  2773. (return-from #1# @i{expression}))
  2774. #3# (return-from #1#
  2775. (apply #'(lambda @i{arglist1} . @i{body1}) #2#))
  2776. #4# (return-from #1#
  2777. (apply #'(lambda @i{arglist2} . @i{body2}) #2#)))))
  2778. @end example
  2779. Unnamed restarts are generally only useful interactively
  2780. and an interactive option which has no description is of little value.
  2781. Implementations are encouraged to warn if
  2782. an unnamed restart is used and no report information
  2783. is provided
  2784. at compilation time.
  2785. At runtime, this error might be noticed when entering
  2786. the debugger. Since signaling an error would probably cause recursive
  2787. entry into the debugger (causing yet another recursive error, etc.) it is
  2788. suggested that the debugger print some indication of such problems when
  2789. they occur but not actually signal errors.
  2790. @example
  2791. (restart-case (signal fred)
  2792. (a ...)
  2793. (b ...))
  2794. @equiv{}
  2795. (restart-case
  2796. (with-condition-restarts fred
  2797. (list (find-restart 'a)
  2798. (find-restart 'b))
  2799. (signal fred))
  2800. (a ...)
  2801. (b ...))
  2802. @end example
  2803. @node restart-name, with-condition-restarts, restart-case, Conditions Dictionary
  2804. @subsection restart-name [Function]
  2805. @code{restart-name} @i{restart} @result{} @i{name}
  2806. @subsubheading Arguments and Values::
  2807. @i{restart}---a @i{restart}.
  2808. @i{name}---a @i{symbol}.
  2809. @subsubheading Description::
  2810. Returns the name of the @i{restart},
  2811. or @b{nil} if the @i{restart} is not named.
  2812. @subsubheading Examples::
  2813. @example
  2814. (restart-case
  2815. (loop for restart in (compute-restarts)
  2816. collect (restart-name restart))
  2817. (case1 () :report "Return 1." 1)
  2818. (nil () :report "Return 2." 2)
  2819. (case3 () :report "Return 3." 3)
  2820. (case1 () :report "Return 4." 4))
  2821. @result{} (CASE1 NIL CASE3 CASE1 ABORT)
  2822. ;; In the example above the restart named ABORT was not created
  2823. ;; explicitly, but was implicitly supplied by the system.
  2824. @end example
  2825. @subsubheading See Also::
  2826. @ref{compute-restarts}
  2827. @ref{find-restart}
  2828. @node with-condition-restarts, with-simple-restart, restart-name, Conditions Dictionary
  2829. @subsection with-condition-restarts [Macro]
  2830. @code{with-condition-restarts} @i{condition-form restarts-form @{@i{form}@}{*}}@*
  2831. @result{} @i{@{@i{result}@}{*}}
  2832. @subsubheading Arguments and Values::
  2833. @i{condition-form}---a @i{form}; @i{evaluated} to produce a @i{condition}.
  2834. @i{condition}---a @i{condition} @i{object} resulting from the
  2835. @i{evaluation} of @i{condition-form}.
  2836. @i{restart-form}---a @i{form}; @i{evaluated} to produce a @i{restart-list}.
  2837. @i{restart-list}---a @i{list} of @i{restart} @i{objects} resulting
  2838. from the @i{evaluation} of @i{restart-form}.
  2839. @i{forms}---an @i{implicit progn}; evaluated.
  2840. @i{results}---the @i{values} returned by @i{forms}.
  2841. @subsubheading Description::
  2842. First, the @i{condition-form} and @i{restarts-form} are @i{evaluated}
  2843. in normal left-to-right order; the @i{primary values} yielded by these
  2844. @i{evaluations} are respectively called the @i{condition}
  2845. and the @i{restart-list}.
  2846. Next, the @i{forms} are @i{evaluated} in a @i{dynamic environment}
  2847. in which each @i{restart} in @i{restart-list} is associated with
  2848. the @i{condition}. See @ref{Associating a Restart with a Condition}.
  2849. @subsubheading See Also::
  2850. @ref{restart-case}
  2851. @subsubheading Notes::
  2852. Usually this @i{macro} is not used explicitly in code,
  2853. since @b{restart-case} handles most of the common cases
  2854. in a way that is syntactically more concise.
  2855. @node with-simple-restart, abort, with-condition-restarts, Conditions Dictionary
  2856. @subsection with-simple-restart [Macro]
  2857. @code{with-simple-restart} @i{@r{(}name format-control @{@i{format-argument}@}{*}@r{)}
  2858. @{@i{form}@}{*}}@*
  2859. @result{} @i{@{@i{result}@}{*}}
  2860. @subsubheading Arguments and Values::
  2861. @i{name}---a @i{symbol}.
  2862. @i{format-control}---a @i{format control}.
  2863. @i{format-argument}---an @i{object} (@i{i.e.}, a @i{format argument}).
  2864. @i{forms}---an @i{implicit progn}.
  2865. @i{results}---in the normal situation,
  2866. the @i{values} returned by the @i{forms};
  2867. in the exceptional situation where the @i{restart} named @i{name} is invoked,
  2868. two values---@b{nil} and @b{t}.
  2869. @subsubheading Description::
  2870. @b{with-simple-restart} establishes a restart.
  2871. If the restart designated by @i{name} is not invoked while executing @i{forms},
  2872. all values returned by the last of @i{forms} are returned.
  2873. If the restart designated by @i{name} is invoked,
  2874. control is transferred to @b{with-simple-restart},
  2875. which returns two values, @b{nil} and @b{t}.
  2876. If @i{name} is @b{nil}, an anonymous restart is established.
  2877. The @i{format-control} and @i{format-arguments} are used
  2878. report the @i{restart}.
  2879. @subsubheading Examples::
  2880. @example
  2881. (defun read-eval-print-loop (level)
  2882. (with-simple-restart (abort "Exit command level ~D." level)
  2883. (loop
  2884. (with-simple-restart (abort "Return to command level ~D." level)
  2885. (let ((form (prog2 (fresh-line) (read) (fresh-line))))
  2886. (prin1 (eval form)))))))
  2887. @result{} READ-EVAL-PRINT-LOOP
  2888. (read-eval-print-loop 1)
  2889. (+ 'a 3)
  2890. @t{ |> } Error: The argument, A, to the function + was of the wrong type.
  2891. @t{ |> } The function expected a number.
  2892. @t{ |> } To continue, type :CONTINUE followed by an option number:
  2893. @t{ |> } 1: Specify a value to use this time.
  2894. @t{ |> } 2: Return to command level 1.
  2895. @t{ |> } 3: Exit command level 1.
  2896. @t{ |> } 4: Return to Lisp Toplevel.
  2897. @end example
  2898. @example
  2899. (defun compute-fixnum-power-of-2 (x)
  2900. (with-simple-restart (nil "Give up on computing 2{@t{^}}~D." x)
  2901. (let ((result 1))
  2902. (dotimes (i x result)
  2903. (setq result (* 2 result))
  2904. (unless (fixnump result)
  2905. (error "Power of 2 is too large."))))))
  2906. COMPUTE-FIXNUM-POWER-OF-2
  2907. (defun compute-power-of-2 (x)
  2908. (or (compute-fixnum-power-of-2 x) 'something big))
  2909. COMPUTE-POWER-OF-2
  2910. (compute-power-of-2 10)
  2911. 1024
  2912. (compute-power-of-2 10000)
  2913. @t{ |> } Error: Power of 2 is too large.
  2914. @t{ |> } To continue, type :CONTINUE followed by an option number.
  2915. @t{ |> } 1: Give up on computing 2{@t{^}}10000.
  2916. @t{ |> } 2: Return to Lisp Toplevel
  2917. @t{ |> } Debug> @b{|>>}@t{:continue 1}@b{<<|}
  2918. @result{} SOMETHING-BIG
  2919. @end example
  2920. @subsubheading See Also::
  2921. @ref{restart-case}
  2922. @subsubheading Notes::
  2923. @b{with-simple-restart} is shorthand for one of the most
  2924. common uses of @b{restart-case}.
  2925. @b{with-simple-restart} could be defined by:
  2926. @example
  2927. (defmacro with-simple-restart ((restart-name format-control
  2928. &rest format-arguments)
  2929. &body forms)
  2930. `(restart-case (progn ,@@forms)
  2931. (,restart-name ()
  2932. :report (lambda (stream)
  2933. (format stream ,format-control ,@@format-arguments))
  2934. (values nil t))))
  2935. @end example
  2936. Because the second return value is @b{t} in the exceptional case,
  2937. it is common (but not required) to arrange for the second return value
  2938. in the normal case to be missing or @b{nil} so that the two situations
  2939. can be distinguished.
  2940. @node abort, continue, with-simple-restart, Conditions Dictionary
  2941. @subsection abort [Restart]
  2942. @subsubheading Data Arguments Required::
  2943. None.
  2944. @subsubheading Description::
  2945. The intent of the @b{abort} restart is to allow return to the
  2946. innermost ``command level.'' Implementors are encouraged to make
  2947. sure that there is always a restart named @b{abort}
  2948. around any user code so that user code can call @b{abort}
  2949. at any time and expect something reasonable to happen;
  2950. exactly what the reasonable thing is may vary somewhat. Typically,
  2951. in an interactive listener, the invocation of @b{abort}
  2952. returns to the @i{Lisp reader} phase of the @i{Lisp read-eval-print loop},
  2953. though in some batch or multi-processing
  2954. situations there may be situations in which having it kill the running
  2955. process is more appropriate.
  2956. @subsubheading See Also::
  2957. @ref{Restarts},
  2958. @ref{Interfaces to Restarts},
  2959. @ref{invoke-restart}
  2960. ,
  2961. @ref{abort}
  2962. (@i{function})
  2963. @node continue, muffle-warning, abort, Conditions Dictionary
  2964. @subsection continue [Restart]
  2965. @subsubheading Data Arguments Required::
  2966. None.
  2967. @subsubheading Description::
  2968. The @b{continue} @i{restart} is generally part of protocols where there is
  2969. a single ``obvious'' way to continue, such as in
  2970. @b{break} and @b{cerror}. Some
  2971. user-defined protocols may also wish to incorporate it for similar reasons.
  2972. In general, however, it is more reliable to design a special purpose restart
  2973. with a name that more directly suits the particular application.
  2974. @subsubheading Examples::
  2975. @example
  2976. (let ((x 3))
  2977. (handler-bind ((error #'(lambda (c)
  2978. (let ((r (find-restart 'continue c)))
  2979. (when r (invoke-restart r))))))
  2980. (cond ((not (floatp x))
  2981. (cerror "Try floating it." "~D is not a float." x)
  2982. (float x))
  2983. (t x)))) @result{} 3.0
  2984. @end example
  2985. @subsubheading See Also::
  2986. @ref{Restarts},
  2987. @ref{Interfaces to Restarts},
  2988. @ref{invoke-restart}
  2989. ,
  2990. @ref{continue}
  2991. (@i{function}),
  2992. @ref{assert}
  2993. ,
  2994. @ref{cerror}
  2995. @node muffle-warning, store-value, continue, Conditions Dictionary
  2996. @subsection muffle-warning [Restart]
  2997. @subsubheading Data Arguments Required::
  2998. None.
  2999. @subsubheading Description::
  3000. This @i{restart} is established by @b{warn} so that @i{handlers}
  3001. of @b{warning} @i{conditions} have a way to tell @b{warn}
  3002. that a warning has already been dealt with and that no further action is warranted.
  3003. @subsubheading Examples::
  3004. @example
  3005. (defvar *all-quiet* nil) @result{} *ALL-QUIET*
  3006. (defvar *saved-warnings* '()) @result{} *SAVED-WARNINGS*
  3007. (defun quiet-warning-handler (c)
  3008. (when *all-quiet*
  3009. (let ((r (find-restart 'muffle-warning c)))
  3010. (when r
  3011. (push c *saved-warnings*)
  3012. (invoke-restart r)))))
  3013. @result{} CUSTOM-WARNING-HANDLER
  3014. (defmacro with-quiet-warnings (&body forms)
  3015. `(let ((*all-quiet* t)
  3016. (*saved-warnings* '()))
  3017. (handler-bind ((warning #'quiet-warning-handler))
  3018. ,@@forms
  3019. *saved-warnings*)))
  3020. @result{} WITH-QUIET-WARNINGS
  3021. (setq saved
  3022. (with-quiet-warnings
  3023. (warn "Situation #1.")
  3024. (let ((*all-quiet* nil))
  3025. (warn "Situation #2."))
  3026. (warn "Situation #3.")))
  3027. @t{ |> } Warning: Situation #2.
  3028. @result{} (#<SIMPLE-WARNING 42744421> #<SIMPLE-WARNING 42744365>)
  3029. (dolist (s saved) (format t "~&~A~
  3030. @t{ |> } Situation #3.
  3031. @t{ |> } Situation #1.
  3032. @result{} NIL
  3033. @end example
  3034. @subsubheading See Also::
  3035. @ref{Restarts},
  3036. @ref{Interfaces to Restarts},
  3037. @ref{invoke-restart}
  3038. ,
  3039. @ref{muffle-warning}
  3040. (@i{function}),
  3041. @ref{warn}
  3042. @node store-value, use-value, muffle-warning, Conditions Dictionary
  3043. @subsection store-value [Restart]
  3044. @subsubheading Data Arguments Required::
  3045. a value to use instead (on an ongoing basis).
  3046. @subsubheading Description::
  3047. The @b{store-value} @i{restart} is generally used by @i{handlers}
  3048. trying to recover from errors of @i{types} such as @b{cell-error}
  3049. or @b{type-error}, which may wish to supply a replacement datum to
  3050. be stored permanently.
  3051. @subsubheading Examples::
  3052. @example
  3053. (defun type-error-auto-coerce (c)
  3054. (when (typep c 'type-error)
  3055. (let ((r (find-restart 'store-value c)))
  3056. (handler-case (let ((v (coerce (type-error-datum c)
  3057. (type-error-expected-type c))))
  3058. (invoke-restart r v))
  3059. (error ()))))) @result{} TYPE-ERROR-AUTO-COERCE
  3060. (let ((x 3))
  3061. (handler-bind ((type-error #'type-error-auto-coerce))
  3062. (check-type x float)
  3063. x)) @result{} 3.0
  3064. @end example
  3065. @subsubheading See Also::
  3066. @ref{Restarts},
  3067. @ref{Interfaces to Restarts},
  3068. @ref{invoke-restart}
  3069. ,
  3070. @ref{store-value}
  3071. (@i{function}),
  3072. @b{ccase},
  3073. @ref{check-type}
  3074. ,
  3075. @b{ctypecase},
  3076. @ref{use-value}
  3077. (@i{function} and @i{restart})
  3078. @node use-value, abort, store-value, Conditions Dictionary
  3079. @subsection use-value [Restart]
  3080. @subsubheading Data Arguments Required::
  3081. a value to use instead (once).
  3082. @subsubheading Description::
  3083. The @b{use-value} @i{restart} is generally used by @i{handlers} trying
  3084. to recover from errors of @i{types} such as @b{cell-error},
  3085. where the handler may wish to supply a replacement datum for one-time use.
  3086. @subsubheading See Also::
  3087. @ref{Restarts},
  3088. @ref{Interfaces to Restarts},
  3089. @ref{invoke-restart}
  3090. ,
  3091. @ref{use-value}
  3092. (@i{function}),
  3093. @ref{store-value}
  3094. (@i{function} and @i{restart})
  3095. @node abort, , use-value, Conditions Dictionary
  3096. @subsection abort, continue, muffle-warning, store-value, use-value [Function]
  3097. @IRindex{abort}
  3098. @IRindex{continue}
  3099. @IRindex{muffle-warning}
  3100. @IRindex{store-value}
  3101. @IRindex{use-value}
  3102. @code{abort} @i{{&optional} condition}
  3103. @result{} #<NoValue>
  3104. @code{continue} @i{{&optional} condition} @result{} @i{@b{nil}}
  3105. @code{muffle-warning} @i{{&optional} condition}
  3106. @result{} #<NoValue>
  3107. @code{store-value} @i{value {&optional} condition} @result{} @i{@b{nil}}
  3108. @code{use-value} @i{value {&optional} condition} @result{} @i{@b{nil}}
  3109. @subsubheading Arguments and Values::
  3110. @i{value}---an @i{object}.
  3111. @i{condition}---a @i{condition} @i{object}, or @b{nil}.
  3112. @subsubheading Description::
  3113. Transfers control to the most recently established @i{applicable restart}
  3114. having the same name as the function. That is,
  3115. the @i{function} @b{abort} searches for an @i{applicable} @b{abort} @i{restart},
  3116. the @i{function} @b{continue} searches for an @i{applicable} @b{continue} @i{restart},
  3117. and so on.
  3118. If no such @i{restart} exists,
  3119. the functions
  3120. @b{continue},
  3121. @b{store-value},
  3122. and @b{use-value}
  3123. return @b{nil}, and
  3124. the functions
  3125. @b{abort}
  3126. and @b{muffle-warning}
  3127. signal an error of @i{type} @b{control-error}.
  3128. When @i{condition} is @i{non-nil},
  3129. only those @i{restarts} are considered that are
  3130. either explicitly associated with that @i{condition},
  3131. or not associated with any @i{condition};
  3132. that is, the excluded @i{restarts} are
  3133. those that are associated with a non-empty set of @i{conditions}
  3134. of which the given @i{condition} is not an @i{element}.
  3135. If @i{condition} is @b{nil}, all @i{restarts} are considered.
  3136. @subsubheading Examples::
  3137. @example
  3138. ;;; Example of the ABORT retart
  3139. (defmacro abort-on-error (&body forms)
  3140. `(handler-bind ((error #'abort))
  3141. ,@@forms)) @result{} ABORT-ON-ERROR
  3142. (abort-on-error (+ 3 5)) @result{} 8
  3143. (abort-on-error (error "You lose."))
  3144. @t{ |> } Returned to Lisp Top Level.
  3145. ;;; Example of the CONTINUE restart
  3146. (defun real-sqrt (n)
  3147. (when (minusp n)
  3148. (setq n (- n))
  3149. (cerror "Return sqrt(~D) instead." "Tried to take sqrt(-~D)." n))
  3150. (sqrt n))
  3151. (real-sqrt 4) @result{} 2
  3152. (real-sqrt -9)
  3153. @t{ |> } Error: Tried to take sqrt(-9).
  3154. @t{ |> } To continue, type :CONTINUE followed by an option number:
  3155. @t{ |> } 1: Return sqrt(9) instead.
  3156. @t{ |> } 2: Return to Lisp Toplevel.
  3157. @t{ |> } Debug> @b{|>>}@t{(continue)}@b{<<|}
  3158. @t{ |> } Return sqrt(9) instead.
  3159. @result{} 3
  3160. (handler-bind ((error #'(lambda (c) (continue))))
  3161. (real-sqrt -9)) @result{} 3
  3162. ;;; Example of the MUFFLE-WARNING restart
  3163. (defun count-down (x)
  3164. (do ((counter x (1- counter)))
  3165. ((= counter 0) 'done)
  3166. (when (= counter 1)
  3167. (warn "Almost done"))
  3168. (format t "~&~D~
  3169. @result{} COUNT-DOWN
  3170. (count-down 3)
  3171. @t{ |> } 3
  3172. @t{ |> } 2
  3173. @t{ |> } Warning: Almost done
  3174. @t{ |> } 1
  3175. @result{} DONE
  3176. (defun ignore-warnings-while-counting (x)
  3177. (handler-bind ((warning #'ignore-warning))
  3178. (count-down x)))
  3179. @result{} IGNORE-WARNINGS-WHILE-COUNTING
  3180. (defun ignore-warning (condition)
  3181. (declare (ignore condition))
  3182. (muffle-warning))
  3183. @result{} IGNORE-WARNING
  3184. (ignore-warnings-while-counting 3)
  3185. @t{ |> } 3
  3186. @t{ |> } 2
  3187. @t{ |> } 1
  3188. @result{} DONE
  3189. ;;; Example of the STORE-VALUE and USE-VALUE restarts
  3190. (defun careful-symbol-value (symbol)
  3191. (check-type symbol symbol)
  3192. (restart-case (if (boundp symbol)
  3193. (return-from careful-symbol-value
  3194. (symbol-value symbol))
  3195. (error 'unbound-variable
  3196. :name symbol))
  3197. (use-value (value)
  3198. :report "Specify a value to use this time."
  3199. value)
  3200. (store-value (value)
  3201. :report "Specify a value to store and use in the future."
  3202. (setf (symbol-value symbol) value))))
  3203. (setq a 1234) @result{} 1234
  3204. (careful-symbol-value 'a) @result{} 1234
  3205. (makunbound 'a) @result{} A
  3206. (careful-symbol-value 'a)
  3207. @t{ |> } Error: A is not bound.
  3208. @t{ |> } To continue, type :CONTINUE followed by an option number.
  3209. @t{ |> } 1: Specify a value to use this time.
  3210. @t{ |> } 2: Specify a value to store and use in the future.
  3211. @t{ |> } 3: Return to Lisp Toplevel.
  3212. @t{ |> } Debug> @b{|>>}@t{(use-value 12)}@b{<<|}
  3213. @result{} 12
  3214. (careful-symbol-value 'a)
  3215. @t{ |> } Error: A is not bound.
  3216. @t{ |> } To continue, type :CONTINUE followed by an option number.
  3217. @t{ |> } 1: Specify a value to use this time.
  3218. @t{ |> } 2: Specify a value to store and use in the future.
  3219. @t{ |> } 3: Return to Lisp Toplevel.
  3220. @t{ |> } Debug> @b{|>>}@t{(store-value 24)}@b{<<|}
  3221. @result{} 24
  3222. (careful-symbol-value 'a)
  3223. @result{} 24
  3224. ;;; Example of the USE-VALUE restart
  3225. (defun add-symbols-with-default (default &rest symbols)
  3226. (handler-bind ((sys:unbound-symbol
  3227. #'(lambda (c)
  3228. (declare (ignore c))
  3229. (use-value default))))
  3230. (apply #'+ (mapcar #'careful-symbol-value symbols))))
  3231. @result{} ADD-SYMBOLS-WITH-DEFAULT
  3232. (setq x 1 y 2) @result{} 2
  3233. (add-symbols-with-default 3 'x 'y 'z) @result{} 6
  3234. @end example
  3235. @subsubheading Side Effects::
  3236. A transfer of control may occur if an appropriate @i{restart} is available,
  3237. or (in the case of the @i{function} @b{abort} or the @i{function} @b{muffle-warning})
  3238. execution may be stopped.
  3239. @subsubheading Affected By::
  3240. Each of these functions can be affected by
  3241. the presence of a @i{restart} having the same name.
  3242. @subsubheading Exceptional Situations::
  3243. If an appropriate @b{abort} @i{restart}
  3244. is not available for the @i{function} @b{abort},
  3245. or an appropriate @b{muffle-warning} @i{restart}
  3246. is not available for the @i{function} @b{muffle-warning},
  3247. an error of @i{type} @b{control-error} is signaled.
  3248. @subsubheading See Also::
  3249. @ref{invoke-restart}
  3250. ,
  3251. @ref{Restarts},
  3252. @ref{Interfaces to Restarts},
  3253. @ref{assert}
  3254. ,
  3255. @b{ccase},
  3256. @ref{cerror}
  3257. ,
  3258. @ref{check-type}
  3259. ,
  3260. @b{ctypecase},
  3261. @ref{use-value}
  3262. ,
  3263. @ref{warn}
  3264. @subsubheading Notes::
  3265. @example
  3266. (abort condition) @equiv{} (invoke-restart 'abort)
  3267. (muffle-warning) @equiv{} (invoke-restart 'muffle-warning)
  3268. (continue) @equiv{} (let ((r (find-restart 'continue))) (if r (invoke-restart r)))
  3269. (use-value @i{x}) @equiv{} (let ((r (find-restart 'use-value))) (if r (invoke-restart r @i{x})))
  3270. (store-value x) @equiv{} (let ((r (find-restart 'store-value))) (if r (invoke-restart r @i{x})))
  3271. @end example
  3272. No functions defined in this specification are required to provide
  3273. a @b{use-value} @i{restart}.
  3274. @c end of including dict-conditions
  3275. @c %**end of chapter