1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
171
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Object subclass:#Exception
|
171
|
14 |
instanceVariableNames:'signal parameter errorString
|
|
15 |
suspendedContext handlerContext
|
|
16 |
rejected
|
|
17 |
resumeBlock rejectBlock'
|
|
18 |
classVariableNames:'EmergencyHandler RecursiveExceptionSignal'
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Kernel-Exceptions'
|
1
|
21 |
!
|
|
22 |
|
|
23 |
Exception comment:'
|
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
171
|
25 |
All Rights Reserved
|
92
|
26 |
|
328
|
27 |
$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.20 1995-05-01 21:29:20 claus Exp $
|
12
|
28 |
'!
|
|
29 |
|
|
30 |
!Exception class methodsFor:'documentation'!
|
|
31 |
|
88
|
32 |
copyright
|
|
33 |
"
|
|
34 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
171
|
35 |
All Rights Reserved
|
88
|
36 |
|
|
37 |
This software is furnished under a license and may be used
|
|
38 |
only in accordance with the terms of that license and with the
|
|
39 |
inclusion of the above copyright notice. This software may not
|
|
40 |
be provided or otherwise made available to, or used by, any
|
|
41 |
other person. No title to or ownership of the software is
|
|
42 |
hereby transferred.
|
|
43 |
"
|
|
44 |
!
|
|
45 |
|
|
46 |
version
|
|
47 |
"
|
328
|
48 |
$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.20 1995-05-01 21:29:20 claus Exp $
|
88
|
49 |
"
|
|
50 |
!
|
|
51 |
|
12
|
52 |
documentation
|
|
53 |
"
|
68
|
54 |
Instances of Exception are passed to a Signal handling block as argument.
|
|
55 |
The handler block may perform various actions by sending corresponding messages
|
142
|
56 |
to the exception object. The following actions are possible:
|
1
|
57 |
|
171
|
58 |
reject - dont handle this signal;
|
|
59 |
another handler will be searched for,
|
|
60 |
upper in the calling hierarchy
|
|
61 |
(this is also the handlers default, if it falls through)
|
1
|
62 |
|
171
|
63 |
proceed - return from the Signal>>raise, with nil as value
|
142
|
64 |
|
171
|
65 |
proceedWith:val - same, but return val from Signal>>raise
|
1
|
66 |
|
171
|
67 |
return - return from the Signal>>handle:do:, with nil as value
|
142
|
68 |
|
171
|
69 |
return:val - same, but return val from Signal>>handle:do:
|
1
|
70 |
|
171
|
71 |
restart - restart the Signal>>handle:do:, after repairing
|
1
|
72 |
|
142
|
73 |
Via the Exception object, the handler can also query the state of execution:
|
68
|
74 |
where the Signal was raised, where the handler is, the signal which caused
|
142
|
75 |
the error and the errorString passed when the signal was raised. Also, an optional
|
|
76 |
parameter can be passed - the use is signal specific.:
|
44
|
77 |
|
68
|
78 |
instance variables:
|
171
|
79 |
signal <Signal> the signal which caused the exception
|
142
|
80 |
|
171
|
81 |
parameter <Object> a parameter (if any) which was passed when raising
|
|
82 |
the signal (only if raised with #raiseWith:aParameter)
|
142
|
83 |
|
171
|
84 |
errorString <String> an errorString
|
|
85 |
(usually the signals own errorString, but sometimes
|
|
86 |
changed explicitely in #raiseWith:errorString:)
|
44
|
87 |
|
171
|
88 |
suspendedContext <Context> the context in which the raise occured
|
142
|
89 |
|
171
|
90 |
handlerContext <Context> the context of the handler (if any)
|
142
|
91 |
|
171
|
92 |
resumeBlock <Block> private to the exception; needed to perform resume action
|
44
|
93 |
|
171
|
94 |
rejectBlock <Block> private to the exception; needed to perform reject action
|
44
|
95 |
|
142
|
96 |
In case of an unhandled signal raise, Exceptions EmergenyHandler will be evaluated.
|
68
|
97 |
The default emergeny handler will enter the debugger.
|
142
|
98 |
|
68
|
99 |
For applications, which do not want Debuggers to come up, other handlers are
|
|
100 |
possible.
|
|
101 |
For example: to get the typical C++ behavior, use:
|
171
|
102 |
Exception emergencyHandler:[:ex | Smalltalk exitWithCoreDump]
|
68
|
103 |
|
|
104 |
Class variables:
|
171
|
105 |
|
|
106 |
EmergencyHandler <Block> this block is evaluated, if no handler was defined
|
|
107 |
for a signal (i.e. this one is responsible for the
|
|
108 |
unhandled exception debugger).
|
|
109 |
Having this being a block allows to globally catch
|
|
110 |
these errors - even when no enclosing handler-scope
|
|
111 |
around the erronous code exists.
|
|
112 |
(as the catch/through does).
|
|
113 |
|
|
114 |
RecursiveExceptionSignal
|
|
115 |
<Signal> raised when within a handler for some signal,
|
|
116 |
th same signal is raised again.
|
12
|
117 |
"
|
|
118 |
! !
|
3
|
119 |
|
171
|
120 |
!Exception class methodsFor:'initialization'!
|
|
121 |
|
|
122 |
initialize
|
|
123 |
"setup the signal used to handle unhandled signals"
|
|
124 |
|
|
125 |
RecursiveExceptionSignal isNil ifTrue:[
|
302
|
126 |
RecursiveExceptionSignal := ErrorSignal newSignalMayProceed:false.
|
171
|
127 |
RecursiveExceptionSignal nameClass:self message:#recursiveExceptionSignal.
|
|
128 |
RecursiveExceptionSignal notifierString:'recursive signal raise in handler'
|
|
129 |
]
|
|
130 |
! !
|
|
131 |
|
|
132 |
!Exception class methodsFor:'signal access'!
|
|
133 |
|
|
134 |
recursiveExceptionSignal
|
|
135 |
"return the signal used to handle recursive signals in the handlers"
|
|
136 |
|
|
137 |
^ RecursiveExceptionSignal
|
|
138 |
! !
|
|
139 |
|
12
|
140 |
!Exception class methodsFor:'defaults'!
|
|
141 |
|
|
142 |
emergencyHandler
|
|
143 |
"return the handler used for unhandled exceptions"
|
|
144 |
|
77
|
145 |
"
|
|
146 |
set it up, when called the first time
|
|
147 |
"
|
|
148 |
EmergencyHandler isNil ifTrue:[
|
171
|
149 |
EmergencyHandler := [:ex |
|
|
150 |
"
|
|
151 |
sending it to the signal allows per-signal specific
|
|
152 |
debuggers to be implemented in the future
|
|
153 |
(for example, segv in primitive code could show things
|
|
154 |
on the C-level ..)
|
|
155 |
"
|
|
156 |
(ex signal) enterDebuggerWith:ex message:(ex errorString).
|
|
157 |
]
|
77
|
158 |
].
|
|
159 |
|
12
|
160 |
^ EmergencyHandler
|
|
161 |
!
|
|
162 |
|
130
|
163 |
emergencyHandler:aOneArgBlock
|
12
|
164 |
"set the handler used for unhandled exceptions"
|
|
165 |
|
130
|
166 |
EmergencyHandler := aOneArgBlock
|
12
|
167 |
! !
|
1
|
168 |
|
|
169 |
!Exception methodsFor:'accessing'!
|
|
170 |
|
|
171 |
signal
|
|
172 |
"return the signal, that caused the exception"
|
|
173 |
|
|
174 |
^ signal
|
|
175 |
!
|
|
176 |
|
44
|
177 |
parameter
|
|
178 |
"return the parameter passsed with the signal raise
|
|
179 |
(or nil, if there was none)"
|
1
|
180 |
|
|
181 |
^ parameter
|
|
182 |
!
|
|
183 |
|
44
|
184 |
errorString
|
|
185 |
"return the errorString passsed with the signal raise
|
|
186 |
(or nil, if there was none)"
|
|
187 |
|
|
188 |
^ errorString
|
|
189 |
!
|
|
190 |
|
|
191 |
handlerContext
|
|
192 |
"return the context of the handler"
|
|
193 |
|
|
194 |
^ handlerContext
|
|
195 |
!
|
|
196 |
|
1
|
197 |
suspendedContext
|
44
|
198 |
"return the context in which the raise occured"
|
|
199 |
|
|
200 |
^ suspendedContext
|
68
|
201 |
!
|
|
202 |
|
|
203 |
rejected
|
171
|
204 |
"return true, if the exception handler rejected
|
|
205 |
(in contrast to a handler falling through).
|
|
206 |
Uncertain, if thais is really interresting to anybody.
|
|
207 |
Notice: this is only valid immediately after handler execution."
|
68
|
208 |
|
|
209 |
^ rejected
|
1
|
210 |
! !
|
|
211 |
|
|
212 |
!Exception methodsFor:'setup'!
|
|
213 |
|
44
|
214 |
signal:aSignal parameter:aParameter errorString:aString suspendedContext:sContext
|
|
215 |
"set the fields usable for inspection by the handler
|
|
216 |
- only to be sent from the signal when raising"
|
1
|
217 |
|
44
|
218 |
signal := aSignal.
|
|
219 |
parameter := aParameter.
|
|
220 |
errorString := aString.
|
|
221 |
suspendedContext := sContext.
|
171
|
222 |
! !
|
|
223 |
|
217
|
224 |
!Exception methodsFor:'raising'!
|
1
|
225 |
|
171
|
226 |
raise
|
|
227 |
"actually raise an exception"
|
1
|
228 |
|
171
|
229 |
resumeBlock := [:value | ^ value].
|
217
|
230 |
^ self evaluateHandler
|
1
|
231 |
!
|
|
232 |
|
171
|
233 |
evaluateHandler
|
|
234 |
"search through the context-calling chain for a 'handle:do:'-context
|
|
235 |
to the raising signal a parent of it or a SignalSet which includes
|
|
236 |
the raising signal.
|
|
237 |
If found, take the contexts 2nd argument (the handler) and evaluate
|
|
238 |
it with the receiver exception as argument.
|
217
|
239 |
If no handler is found, try per signal handler, or
|
|
240 |
per process handler (if its the noHandlerSignal).
|
|
241 |
Finally fall back to Exceptions emergencyHandler, which is always
|
|
242 |
available and enters the debugger."
|
171
|
243 |
|
|
244 |
|con block noHandlerSignal|
|
|
245 |
|
|
246 |
con := thisContext sender. "the raise-context"
|
|
247 |
con := con sender. "the signal raise context"
|
|
248 |
con isRecursive ifTrue:[
|
|
249 |
"
|
|
250 |
mhmh - an error while in a handler
|
|
251 |
"
|
|
252 |
((signal == RecursiveExceptionSignal)
|
|
253 |
or:[RecursiveExceptionSignal isNil]) ifTrue:[
|
|
254 |
"
|
|
255 |
... either while handling RecursiveExceptionSignal
|
|
256 |
or at startup when RecursiveExceptionSignal is not yet
|
|
257 |
created -
|
|
258 |
- go immediately into the debugger.
|
|
259 |
"
|
|
260 |
^ self enterDebuggerWith:self
|
|
261 |
message:'recursive signal raise'
|
|
262 |
].
|
|
263 |
^ RecursiveExceptionSignal
|
|
264 |
raiseRequestWith:self
|
|
265 |
errorString:('recursive signal raise: ' , errorString)
|
|
266 |
].
|
|
267 |
|
|
268 |
[con notNil] whileTrue:[
|
|
269 |
(con selector == #'handle:do:') ifTrue:[
|
|
270 |
"
|
|
271 |
if this is the Signal>>handle:do: context
|
|
272 |
or a SignalSet>>handle:do: context with self in it,
|
|
273 |
call the handler
|
|
274 |
"
|
|
275 |
(con receiver accepts:signal) ifTrue:[
|
|
276 |
"call the handler"
|
|
277 |
|
|
278 |
handlerContext := con.
|
|
279 |
self doCallHandler:(con args at:1).
|
1
|
280 |
|
171
|
281 |
"if the handler rejects or falls through we arrive here"
|
|
282 |
"continue search for another handler"
|
|
283 |
].
|
|
284 |
].
|
|
285 |
con := con sender
|
|
286 |
].
|
|
287 |
|
|
288 |
"
|
|
289 |
we arrive here, if either no handler was found, or none of the
|
|
290 |
handlers did a return (i.e. every handler rejected or fell through).
|
|
291 |
"
|
|
292 |
"
|
|
293 |
try per signal handler
|
|
294 |
"
|
|
295 |
(block := signal handlerBlock) notNil ifTrue:[
|
|
296 |
^ block value:self.
|
|
297 |
].
|
|
298 |
|
|
299 |
"
|
|
300 |
if it is not the NoHandlerSignal, raise it ...
|
213
|
301 |
passing the receiver as parameter.
|
171
|
302 |
"
|
|
303 |
signal ~~ (noHandlerSignal := Signal noHandlerSignal) ifTrue:[
|
|
304 |
noHandlerSignal notNil ifTrue:[
|
|
305 |
^ noHandlerSignal
|
|
306 |
raiseRequestWith:self
|
|
307 |
errorString:('unhandled exception: ' , errorString)
|
255
|
308 |
in:self suspendedContext
|
171
|
309 |
].
|
|
310 |
"
|
|
311 |
mhmh - an error during early startup; noHandlerSignal is
|
|
312 |
not yet defined.
|
|
313 |
"
|
|
314 |
^ MiniDebugger enterWithMessage:errorString
|
|
315 |
].
|
|
316 |
|
|
317 |
"
|
|
318 |
mhmh - smells like trouble - there is no handler and
|
|
319 |
no per-signal handler block.
|
|
320 |
Look for either a per-process emergencyHandlerBlock
|
|
321 |
or the global emergencyHandler (from Exception) ...
|
|
322 |
"
|
|
323 |
Processor notNil ifTrue:[
|
|
324 |
"care for signal during startup (Processor not yet created)"
|
|
325 |
block := Processor activeProcess emergencySignalHandler.
|
|
326 |
].
|
|
327 |
block isNil ifTrue:[
|
|
328 |
block := Exception emergencyHandler
|
|
329 |
].
|
|
330 |
block isNil ifTrue:[
|
|
331 |
"care for error during startup (Exception not yet initialized)"
|
|
332 |
^ MiniDebugger enterWithMessage:errorString
|
|
333 |
].
|
|
334 |
|
|
335 |
"... and call it"
|
|
336 |
^ block value:self.
|
1
|
337 |
!
|
|
338 |
|
171
|
339 |
doCallHandler:aHandler
|
|
340 |
"call the handler proper - needed an extra method
|
|
341 |
to have a separate returnContext for the rejectBlock.
|
|
342 |
(which is historical, and actually no longer needed)"
|
1
|
343 |
|
171
|
344 |
rejectBlock := [^ self]. "this will return on reject"
|
|
345 |
aHandler value:self.
|
|
346 |
"handler fall through - is just like a reject"
|
1
|
347 |
! !
|
|
348 |
|
|
349 |
!Exception methodsFor:'handler actions'!
|
|
350 |
|
|
351 |
reject
|
|
352 |
"handler decided not to handle this signal -
|
|
353 |
system will look for another handler"
|
|
354 |
|
68
|
355 |
rejected := true.
|
1
|
356 |
rejectBlock value
|
|
357 |
!
|
|
358 |
|
|
359 |
resume
|
|
360 |
"Continue after the raise - the raise returns nil"
|
|
361 |
|
|
362 |
resumeBlock value:nil
|
|
363 |
!
|
|
364 |
|
|
365 |
resumeWith:value
|
|
366 |
"Continue after the raise - the raise returns value"
|
|
367 |
|
|
368 |
resumeBlock value:value
|
|
369 |
!
|
|
370 |
|
|
371 |
proceed
|
|
372 |
"Continue after the raise - the raise returns nil"
|
|
373 |
|
|
374 |
resumeBlock value:nil
|
|
375 |
!
|
|
376 |
|
|
377 |
proceedWith:value
|
|
378 |
"Continue after the raise - the raise returns value"
|
|
379 |
|
|
380 |
resumeBlock value:value
|
|
381 |
!
|
|
382 |
|
|
383 |
return
|
|
384 |
"Continue after the handle:do: - the handle:do: returns nil"
|
|
385 |
|
68
|
386 |
handlerContext unwind
|
1
|
387 |
!
|
|
388 |
|
|
389 |
returnWith:value
|
|
390 |
"Continue after the handle:do: - the handle:do: returns value"
|
|
391 |
|
68
|
392 |
handlerContext unwind:value
|
1
|
393 |
!
|
|
394 |
|
328
|
395 |
returnDoing:aBlock
|
|
396 |
"Continue after the handle:do: - the handle:do: returns aBlock value"
|
|
397 |
|
|
398 |
handlerContext unwindThenDo:aBlock
|
|
399 |
!
|
|
400 |
|
1
|
401 |
restart
|
|
402 |
"restart the handle:do: - usually after some repair work is done
|
|
403 |
in handler"
|
|
404 |
|
249
|
405 |
handlerContext unwindAndRestart
|
1
|
406 |
! !
|