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
|
362
|
16 |
rejected originator
|
171
|
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 |
|
362
|
27 |
$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.22 1995-07-22 19:22:18 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 |
"
|
362
|
48 |
$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.22 1995-07-22 19:22:18 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
|
1
|
61 |
|
171
|
62 |
proceed - return from the Signal>>raise, with nil as value
|
142
|
63 |
|
171
|
64 |
proceedWith:val - same, but return val from Signal>>raise
|
1
|
65 |
|
171
|
66 |
return - return from the Signal>>handle:do:, with nil as value
|
142
|
67 |
|
362
|
68 |
returnWith:val - same, but return val from Signal>>handle:do:
|
|
69 |
(this is also the handlers default,
|
|
70 |
if it falls through; taking the handlerBlocks value
|
|
71 |
as return value)
|
1
|
72 |
|
171
|
73 |
restart - restart the Signal>>handle:do:, after repairing
|
1
|
74 |
|
142
|
75 |
Via the Exception object, the handler can also query the state of execution:
|
68
|
76 |
where the Signal was raised, where the handler is, the signal which caused
|
142
|
77 |
the error and the errorString passed when the signal was raised. Also, an optional
|
|
78 |
parameter can be passed - the use is signal specific.:
|
44
|
79 |
|
68
|
80 |
instance variables:
|
171
|
81 |
signal <Signal> the signal which caused the exception
|
142
|
82 |
|
171
|
83 |
parameter <Object> a parameter (if any) which was passed when raising
|
|
84 |
the signal (only if raised with #raiseWith:aParameter)
|
142
|
85 |
|
171
|
86 |
errorString <String> an errorString
|
|
87 |
(usually the signals own errorString, but sometimes
|
|
88 |
changed explicitely in #raiseWith:errorString:)
|
44
|
89 |
|
171
|
90 |
suspendedContext <Context> the context in which the raise occured
|
142
|
91 |
|
171
|
92 |
handlerContext <Context> the context of the handler (if any)
|
142
|
93 |
|
171
|
94 |
resumeBlock <Block> private to the exception; needed to perform resume action
|
44
|
95 |
|
171
|
96 |
rejectBlock <Block> private to the exception; needed to perform reject action
|
44
|
97 |
|
142
|
98 |
In case of an unhandled signal raise, Exceptions EmergenyHandler will be evaluated.
|
68
|
99 |
The default emergeny handler will enter the debugger.
|
142
|
100 |
|
68
|
101 |
For applications, which do not want Debuggers to come up, other handlers are
|
|
102 |
possible.
|
362
|
103 |
For example, to get the typical C++ behavior, use:
|
171
|
104 |
Exception emergencyHandler:[:ex | Smalltalk exitWithCoreDump]
|
68
|
105 |
|
|
106 |
Class variables:
|
171
|
107 |
|
|
108 |
EmergencyHandler <Block> this block is evaluated, if no handler was defined
|
|
109 |
for a signal (i.e. this one is responsible for the
|
|
110 |
unhandled exception debugger).
|
|
111 |
Having this being a block allows to globally catch
|
|
112 |
these errors - even when no enclosing handler-scope
|
|
113 |
around the erronous code exists.
|
|
114 |
(as the catch/through does).
|
|
115 |
|
|
116 |
RecursiveExceptionSignal
|
|
117 |
<Signal> raised when within a handler for some signal,
|
|
118 |
th same signal is raised again.
|
12
|
119 |
"
|
|
120 |
! !
|
3
|
121 |
|
171
|
122 |
!Exception class methodsFor:'initialization'!
|
|
123 |
|
|
124 |
initialize
|
|
125 |
"setup the signal used to handle unhandled signals"
|
|
126 |
|
|
127 |
RecursiveExceptionSignal isNil ifTrue:[
|
302
|
128 |
RecursiveExceptionSignal := ErrorSignal newSignalMayProceed:false.
|
171
|
129 |
RecursiveExceptionSignal nameClass:self message:#recursiveExceptionSignal.
|
|
130 |
RecursiveExceptionSignal notifierString:'recursive signal raise in handler'
|
|
131 |
]
|
|
132 |
! !
|
|
133 |
|
345
|
134 |
!Exception class methodsFor:'Signal constants'!
|
171
|
135 |
|
|
136 |
recursiveExceptionSignal
|
|
137 |
"return the signal used to handle recursive signals in the handlers"
|
|
138 |
|
|
139 |
^ RecursiveExceptionSignal
|
|
140 |
! !
|
|
141 |
|
12
|
142 |
!Exception class methodsFor:'defaults'!
|
|
143 |
|
|
144 |
emergencyHandler
|
|
145 |
"return the handler used for unhandled exceptions"
|
|
146 |
|
77
|
147 |
"
|
|
148 |
set it up, when called the first time
|
|
149 |
"
|
|
150 |
EmergencyHandler isNil ifTrue:[
|
171
|
151 |
EmergencyHandler := [:ex |
|
|
152 |
"
|
|
153 |
sending it to the signal allows per-signal specific
|
|
154 |
debuggers to be implemented in the future
|
|
155 |
(for example, segv in primitive code could show things
|
|
156 |
on the C-level ..)
|
|
157 |
"
|
|
158 |
(ex signal) enterDebuggerWith:ex message:(ex errorString).
|
|
159 |
]
|
77
|
160 |
].
|
|
161 |
|
12
|
162 |
^ EmergencyHandler
|
|
163 |
!
|
|
164 |
|
130
|
165 |
emergencyHandler:aOneArgBlock
|
12
|
166 |
"set the handler used for unhandled exceptions"
|
|
167 |
|
130
|
168 |
EmergencyHandler := aOneArgBlock
|
362
|
169 |
|
|
170 |
"ST-80 behavior of first showing a notifier:
|
|
171 |
(I prefer to get right into the debugger, though)
|
|
172 |
|
|
173 |
Exception
|
|
174 |
emergencyHandler:
|
|
175 |
[:ex | self notify:ex errorString ]
|
|
176 |
"
|
|
177 |
|
|
178 |
"automatically aborting current operation, on error:
|
|
179 |
(may be useful for end-user apps; make certain,
|
|
180 |
you have abortSignal handlers at appropriate places)
|
|
181 |
|
|
182 |
Exception
|
|
183 |
emergencyHandler:
|
|
184 |
[:ex | Object abortSignal raise. ex return. ]
|
|
185 |
"
|
|
186 |
|
|
187 |
"finally, traditional language system behavior; dump core ;-)
|
|
188 |
|
|
189 |
Exception
|
|
190 |
emergencyHandler:
|
|
191 |
[:ex | Smalltalk exitWithCoreDump. ]
|
|
192 |
"
|
|
193 |
! !
|
|
194 |
|
|
195 |
!Exception class methodsFor:'instance creation'!
|
|
196 |
|
|
197 |
signal:aSignal parameter:aParameter errorString:aString suspendedContext:sContext originator:origin
|
|
198 |
"create a new instance and set the fields in preparation for a raise.
|
|
199 |
- only to be sent from the signal when raising"
|
|
200 |
|
|
201 |
^ (self new)
|
|
202 |
signal:aSignal
|
|
203 |
parameter:aParameter
|
|
204 |
errorString:aString
|
|
205 |
suspendedContext:sContext
|
|
206 |
originator:origin.
|
|
207 |
!
|
|
208 |
|
|
209 |
signal:aSignal parameter:aParameter errorString:aString suspendedContext:sContext
|
|
210 |
"create a new instance and set the fields in preparation for a raise.
|
|
211 |
- only to be sent from the signal when raising"
|
|
212 |
|
|
213 |
^ (self new)
|
|
214 |
signal:aSignal
|
|
215 |
parameter:aParameter
|
|
216 |
errorString:aString
|
|
217 |
suspendedContext:sContext
|
|
218 |
originator:(sContext receiver).
|
12
|
219 |
! !
|
1
|
220 |
|
|
221 |
!Exception methodsFor:'accessing'!
|
|
222 |
|
|
223 |
signal
|
|
224 |
"return the signal, that caused the exception"
|
|
225 |
|
|
226 |
^ signal
|
|
227 |
!
|
|
228 |
|
44
|
229 |
parameter
|
|
230 |
"return the parameter passsed with the signal raise
|
|
231 |
(or nil, if there was none)"
|
1
|
232 |
|
|
233 |
^ parameter
|
|
234 |
!
|
|
235 |
|
44
|
236 |
errorString
|
|
237 |
"return the errorString passsed with the signal raise
|
|
238 |
(or nil, if there was none)"
|
|
239 |
|
|
240 |
^ errorString
|
|
241 |
!
|
|
242 |
|
362
|
243 |
originator
|
|
244 |
"return the originator passsed with the signal raise
|
|
245 |
(or nil, if there was none)"
|
|
246 |
|
|
247 |
^ originator
|
|
248 |
!
|
|
249 |
|
44
|
250 |
handlerContext
|
|
251 |
"return the context of the handler"
|
|
252 |
|
|
253 |
^ handlerContext
|
|
254 |
!
|
|
255 |
|
1
|
256 |
suspendedContext
|
44
|
257 |
"return the context in which the raise occured"
|
|
258 |
|
|
259 |
^ suspendedContext
|
68
|
260 |
!
|
|
261 |
|
|
262 |
rejected
|
362
|
263 |
"return true, if any other of the exceptions handlers has rejected
|
|
264 |
Uncertain, if this is really interresting to anybody.
|
|
265 |
This is only valid during handler execution.
|
|
266 |
(i.e. an outer handler can find out, if any other handler has already
|
|
267 |
rejected).
|
|
268 |
"
|
68
|
269 |
|
|
270 |
^ rejected
|
362
|
271 |
!
|
|
272 |
|
|
273 |
willProceed
|
|
274 |
"return true, if the exception is proceedable"
|
|
275 |
|
|
276 |
^ resumeBlock notNil
|
1
|
277 |
! !
|
|
278 |
|
|
279 |
!Exception methodsFor:'setup'!
|
|
280 |
|
362
|
281 |
signal:aSignal parameter:aParameter errorString:aString suspendedContext:sContext originator:origin
|
44
|
282 |
"set the fields usable for inspection by the handler
|
|
283 |
- only to be sent from the signal when raising"
|
1
|
284 |
|
44
|
285 |
signal := aSignal.
|
|
286 |
parameter := aParameter.
|
|
287 |
errorString := aString.
|
|
288 |
suspendedContext := sContext.
|
362
|
289 |
originator := origin.
|
171
|
290 |
! !
|
|
291 |
|
217
|
292 |
!Exception methodsFor:'raising'!
|
1
|
293 |
|
171
|
294 |
raise
|
362
|
295 |
"actually raise a nonproceedable exception.
|
|
296 |
For now, same as #raiseRequest (always proceedable)."
|
|
297 |
|
|
298 |
resumeBlock := [:value | ^ value].
|
|
299 |
^ self evaluateHandler
|
|
300 |
!
|
|
301 |
|
|
302 |
raiseRequest
|
|
303 |
"actually raise a proceedable exception."
|
1
|
304 |
|
171
|
305 |
resumeBlock := [:value | ^ value].
|
217
|
306 |
^ self evaluateHandler
|
1
|
307 |
!
|
|
308 |
|
171
|
309 |
evaluateHandler
|
|
310 |
"search through the context-calling chain for a 'handle:do:'-context
|
|
311 |
to the raising signal a parent of it or a SignalSet which includes
|
|
312 |
the raising signal.
|
|
313 |
If found, take the contexts 2nd argument (the handler) and evaluate
|
|
314 |
it with the receiver exception as argument.
|
217
|
315 |
If no handler is found, try per signal handler, or
|
|
316 |
per process handler (if its the noHandlerSignal).
|
|
317 |
Finally fall back to Exceptions emergencyHandler, which is always
|
|
318 |
available and enters the debugger."
|
171
|
319 |
|
362
|
320 |
|con block noHandlerSignal any msg sel|
|
171
|
321 |
|
|
322 |
con := thisContext sender. "the raise-context"
|
|
323 |
con := con sender. "the signal raise context"
|
|
324 |
con isRecursive ifTrue:[
|
|
325 |
"
|
|
326 |
mhmh - an error while in a handler
|
|
327 |
"
|
|
328 |
((signal == RecursiveExceptionSignal)
|
|
329 |
or:[RecursiveExceptionSignal isNil]) ifTrue:[
|
|
330 |
"
|
|
331 |
... either while handling RecursiveExceptionSignal
|
|
332 |
or at startup when RecursiveExceptionSignal is not yet
|
|
333 |
created -
|
|
334 |
- go immediately into the debugger.
|
|
335 |
"
|
|
336 |
^ self enterDebuggerWith:self
|
|
337 |
message:'recursive signal raise'
|
|
338 |
].
|
|
339 |
^ RecursiveExceptionSignal
|
|
340 |
raiseRequestWith:self
|
|
341 |
errorString:('recursive signal raise: ' , errorString)
|
|
342 |
].
|
|
343 |
|
362
|
344 |
any := false.
|
171
|
345 |
[con notNil] whileTrue:[
|
362
|
346 |
con isBlockContext ifFalse:[
|
|
347 |
(((sel := con selector) == #'handle:do:')
|
|
348 |
or:[((sel == #'handle:from:do:')
|
|
349 |
and:[(con args at:2) == originator])]) ifTrue:[
|
|
350 |
"
|
|
351 |
if this is the Signal>>handle:do: context
|
|
352 |
or a SignalSet>>handle:do: context with self in it,
|
|
353 |
call the handler
|
|
354 |
"
|
|
355 |
(con receiver accepts:signal) ifTrue:[
|
|
356 |
"call the handler"
|
171
|
357 |
|
362
|
358 |
handlerContext := con.
|
|
359 |
any := true.
|
|
360 |
self doCallHandler:(con args at:1).
|
1
|
361 |
|
362
|
362 |
"if the handler rejects, we arrive here"
|
|
363 |
"continue search for another handler"
|
|
364 |
].
|
|
365 |
]
|
171
|
366 |
].
|
|
367 |
con := con sender
|
|
368 |
].
|
|
369 |
|
|
370 |
"
|
|
371 |
we arrive here, if either no handler was found, or none of the
|
|
372 |
handlers did a return (i.e. every handler rejected or fell through).
|
|
373 |
"
|
|
374 |
"
|
|
375 |
try per signal handler
|
|
376 |
"
|
|
377 |
(block := signal handlerBlock) notNil ifTrue:[
|
|
378 |
^ block value:self.
|
|
379 |
].
|
|
380 |
|
|
381 |
"
|
|
382 |
if it is not the NoHandlerSignal, raise it ...
|
213
|
383 |
passing the receiver as parameter.
|
171
|
384 |
"
|
|
385 |
signal ~~ (noHandlerSignal := Signal noHandlerSignal) ifTrue:[
|
|
386 |
noHandlerSignal notNil ifTrue:[
|
362
|
387 |
any ifTrue:[
|
|
388 |
msg := 'unhandled (rejected)'
|
|
389 |
] ifFalse:[
|
|
390 |
msg := 'unhandled'
|
|
391 |
].
|
171
|
392 |
^ noHandlerSignal
|
|
393 |
raiseRequestWith:self
|
362
|
394 |
errorString:(msg , ' exception: ' , errorString)
|
255
|
395 |
in:self suspendedContext
|
171
|
396 |
].
|
|
397 |
"
|
|
398 |
mhmh - an error during early startup; noHandlerSignal is
|
|
399 |
not yet defined.
|
|
400 |
"
|
|
401 |
^ MiniDebugger enterWithMessage:errorString
|
|
402 |
].
|
|
403 |
|
|
404 |
"
|
|
405 |
mhmh - smells like trouble - there is no handler and
|
|
406 |
no per-signal handler block.
|
|
407 |
Look for either a per-process emergencyHandlerBlock
|
|
408 |
or the global emergencyHandler (from Exception) ...
|
|
409 |
"
|
|
410 |
Processor notNil ifTrue:[
|
|
411 |
"care for signal during startup (Processor not yet created)"
|
|
412 |
block := Processor activeProcess emergencySignalHandler.
|
|
413 |
].
|
|
414 |
block isNil ifTrue:[
|
|
415 |
block := Exception emergencyHandler
|
|
416 |
].
|
|
417 |
block isNil ifTrue:[
|
|
418 |
"care for error during startup (Exception not yet initialized)"
|
|
419 |
^ MiniDebugger enterWithMessage:errorString
|
|
420 |
].
|
|
421 |
|
|
422 |
"... and call it"
|
|
423 |
^ block value:self.
|
1
|
424 |
!
|
|
425 |
|
171
|
426 |
doCallHandler:aHandler
|
|
427 |
"call the handler proper - needed an extra method
|
|
428 |
to have a separate returnContext for the rejectBlock.
|
|
429 |
(which is historical, and actually no longer needed)"
|
1
|
430 |
|
362
|
431 |
|val|
|
|
432 |
|
171
|
433 |
rejectBlock := [^ self]. "this will return on reject"
|
362
|
434 |
val := aHandler value:self.
|
|
435 |
"
|
|
436 |
handler fall through - is just like a returnWith:blocks-value
|
|
437 |
"
|
|
438 |
self returnWith:val
|
1
|
439 |
! !
|
|
440 |
|
|
441 |
!Exception methodsFor:'handler actions'!
|
|
442 |
|
|
443 |
reject
|
|
444 |
"handler decided not to handle this signal -
|
|
445 |
system will look for another handler"
|
|
446 |
|
68
|
447 |
rejected := true.
|
1
|
448 |
rejectBlock value
|
|
449 |
!
|
|
450 |
|
|
451 |
resume
|
|
452 |
"Continue after the raise - the raise returns nil"
|
|
453 |
|
362
|
454 |
resumeBlock notNil ifTrue:[resumeBlock value:nil]
|
1
|
455 |
!
|
|
456 |
|
|
457 |
resumeWith:value
|
|
458 |
"Continue after the raise - the raise returns value"
|
|
459 |
|
362
|
460 |
resumeBlock notNil ifTrue:[resumeBlock value:value]
|
1
|
461 |
!
|
|
462 |
|
|
463 |
proceed
|
|
464 |
"Continue after the raise - the raise returns nil"
|
|
465 |
|
362
|
466 |
resumeBlock notNil ifTrue:[resumeBlock value:nil]
|
1
|
467 |
!
|
|
468 |
|
|
469 |
proceedWith:value
|
|
470 |
"Continue after the raise - the raise returns value"
|
|
471 |
|
362
|
472 |
resumeBlock notNil ifTrue:[resumeBlock value:value]
|
1
|
473 |
!
|
|
474 |
|
|
475 |
return
|
|
476 |
"Continue after the handle:do: - the handle:do: returns nil"
|
|
477 |
|
68
|
478 |
handlerContext unwind
|
1
|
479 |
!
|
|
480 |
|
|
481 |
returnWith:value
|
|
482 |
"Continue after the handle:do: - the handle:do: returns value"
|
|
483 |
|
68
|
484 |
handlerContext unwind:value
|
1
|
485 |
!
|
|
486 |
|
328
|
487 |
returnDoing:aBlock
|
|
488 |
"Continue after the handle:do: - the handle:do: returns aBlock value"
|
|
489 |
|
|
490 |
handlerContext unwindThenDo:aBlock
|
|
491 |
!
|
|
492 |
|
1
|
493 |
restart
|
|
494 |
"restart the handle:do: - usually after some repair work is done
|
|
495 |
in handler"
|
|
496 |
|
249
|
497 |
handlerContext unwindAndRestart
|
1
|
498 |
! !
|