fix font switch
[unix-history] / usr / src / old / lisp / PSD.doc / ch10.n
CommitLineData
3603109f
NC
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
f37da2f3 5.\" @(#)ch10.n 6.1 (Berkeley) %G%
3603109f 6.\"
f37da2f3
NC
7." $Header: /na/franz/doc/RCS/ch10.n,v 1.1 83/01/31 07:08:20 jkf Exp $
8.Lc Exception\ Handling 10
9.sh 2 Errset\ and\ Error\ Handler\ Functions 10
10.pp
11.Fr
12allows the user to handle in a number of ways the errors
13which arise during computation.
14One way is through the use of the
15.i errset
16function.
17If an error occurs during the evaluation of the
18.i errset 's
19first argument, then
20the locus of control will return to the errset which will
21return nil (except in special cases, such as
22.i err ).
23The other method of error handling is through an error handler
24function.
25When an error occurs, the error handler is called and
26is given as an argument a description of the
27error which just occurred.
28The error handler may take one of the following actions:
29.nr $p 0
30.np
31it could take some drastic action like a
32.i reset
33or a
34.i throw .
35.np
36it could, assuming that the error is continuable,
37return
38to the function which noticed the error.
39The error handler indicates that it wants to return a value from
40the error by returning a list whose
41.i car
42is the value it wants to return.
43.np
44it could decide not to handle the error and return a non-list to
45indicate this fact.
46.sh 2 "The Anatomy of an error"
47.pp
48Each error is described by a list of these items:
49.nr $p 0
50.np
51error type - This is a symbol which indicates the
52general classification of the error.
53This classification may determine which function handles this
54error.
55.np
56unique id - This is a fixnum unique to this error.
57.np
58continuable - If this is non-nil then this error is continuable.
59There are some who feel that every error should be continuable
60and the reason that some (in fact most) errors in
61.Fr
62are not continuable is due to the laziness of the programmers.
63.np
64message string - This is a symbol whose print name is a
65message describing the error.
66.np
67data - There may be from zero to three lisp values which help
68describe this particular error.
69For example, the unbound variable error contains one datum value,
70the symbol whose value is unbound.
71The list describing that error might look like:
72.br
73.ce
74(ER%misc 0 t |Unbound Variable:| foobar)
75.sh 2 "Error handling algorithm"
76.pp
77This is the sequence of operations which is done when an
78error occurs:
79.nr $p 0
80.np
81If the symbol
82.b ER%all
83has a non nil value
84then this value is the name of an error handler function.
85That function is called with a description of the error.
86If that function returns (and of course it may choose not to)
87and the value is a list and this error is continuable, then
88we return the
89.i car
90of the list to the function which called the error.
91Presumably the function will use this value to retry the operation.
92On the other hand, if the error handler returns a non list, then
93it has chosen not to handle this error, so we go on to step (2).
94Something special happens before we call the
95.b ER%all
96error
97handler which does not happen in any of the
98other cases we will describe below.
99To help insure that we don't get infinitely recursive
100errors if
101.b ER%all
102is set to a bad value,
103the value of
104.b ER%all
105is set to nil before the
106handler is called.
107Thus it is the responsibility of the
108.b ER%all
109handler to `reenable'
110itself by storing its name in
111.b ER%all.
112.np
113Next the specific error handler for the type of error
114which just occurred is called (if one exists) to see if
115it wants to handle the error.
116The names of the handlers for the specific types of errors are stored
117as the values of the symbols whose names are the types.
118For example the handler for miscellaneous errors is stored as the
119value of
120.b ER%misc.
121Of course, if
122.b ER%misc
123has a value of nil, then there is no error
124handler for this type of error.
125Appendix B contains list of all error types.
126The process of classifying the errors is not complete and thus most
127errors are lumped into the \fBER%misc\fP category.
128Just as in step (1),
129the error handler function may choose not to handle the error
130by returning a non-list, and then we go to step (3).
131.np
132Next a check is made to see if there is an
133.i errset
134surrounding this error.
135If so the second argument to the
136.i errset
137call
138is examined.
139If the second argument was not given or is non nil
140then the error message associated with this error is printed.
141Finally the stack is popped
142to the context of the
143.i errset
144and then the
145.i errset
146returns nil.
147If there was no
148.i errset
149we go to step (4).
150.np
151If the symbol
152.b ER%tpl
153has a value then it is the
154name of an error handler which is called in a manner similar
155to that discussed above.
156If it chooses not to handle the error, we go to step (5).
157.np
158At this point it has been determined that the user doesn't
159want to handle this error.
160Thus the error message is printed out and
161a
162.i reset
163is done to send the flow of control to the top-level.
164.pp
165To summarize the error handling system:
166When an error occurs, you have two chances to handle it before
167the search for an
168.i errset
169is done.
170Then, if there is no
171.i errset ,
172you have one more chance to handle the error before control
173jumps to the top level.
174Every error handler works in the same way:
175It is given a description of the error (as described in the
176previous section).
177It may or may not return.
178If it returns, then it returns
179either a list or a non-list.
180If it returns a list and the error is continuable, then
181the
182.i car
183of the list is returned to the function which noticed the error.
184Otherwise the error handler has decided not to handle the error
185and we go on to something else.
186.sh 2 "Default aids"
187.pp
188There are two standard error handlers which will probably
189handle the needs of most users.
190One of these is the lisp coded function
191.i break-err-handler
192which is the default value of
193.b ER%tpl.
194Thus when all other handlers have ignored an error,
195.i break-err-handler
196will take over.
197It will print out the error message and
198go into a read-eval-print loop.
199The other standard error handler is
200.i debug-err-handler .
201This handler is designed to be connected to
202.b ER%all and
203is useful if your program uses
204.i errset
205and you want to
206look at the error before
207it is thrown up to the
208.i errset .
209.sh +0 Autoloading
210.pp
211When
212.i eval ,
213.i apply
214or
215.i funcall
216are told to call an undefined function, an \fBER%undef\fP
217error is signaled.
218The default handler for this error is
219.i undef-func-handler .
220This function checks the property list of the undefined function for
221the indicator autoload.
222If present, the value of that indicator should be the name of the file
223which contains the definition of the undefined function.
224.i Undef-func-handler
225will load the file and check if it has defined the function which caused
226the error.
227If it has, the error handler will return and the computation will continue
228as if the error did not occur.
229This provides a way for the user to tell the lisp system about the location
230of commonly used functions.
231The trace package sets up an autoload property to point to /usr/lib/lisp/trace.
232.sh +0 Interrupt\ processing
233.pp
234The UNIX operating system provides one user interrupt character which
235defaults to ^C.\*[\(dg\*]
236.(f
237\*[\(dg\*]Actually there are two but the lisp system does not allow you
238to catch the QUIT interrupt.
239.)f
240The user may select a lisp function to run when an interrupt occurs.
241Since this interrupt could occur at any time, and in particular could
242occur at a time when the internal stack pointers were in an inconsistent
243state, the processing of the interrupt may be delayed until a safe
244time.
245When the first ^C is typed, the lisp system sets a flag that an interrupt
246has been requested.
247This flag is checked at safe places within the interpreter
248and in the
249.i qlinker
250function.
251If the lisp system doesn't respond to the first ^C, another ^C should
252be typed.
253This will cause all of the transfer tables to be cleared forcing
254all calls from compiled code to go through the
255.i qlinker
256function where the interrupt flag will be checked.
257If the lisp system still doesn't respond, a third ^C will cause
258an immediate interrupt.
259This interrupt will not necessarily be in a safe place so the
260user should
261.i reset
262the lisp system as soon as possible.