author | Stefan Vogel <sv@exept.de> |
Fri, 23 Jul 1999 09:54:42 +0200 | |
changeset 4438 | 0b01443d07c1 |
parent 3521 | 4d83701cfc28 |
child 4440 | 88843a1c2700 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 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 |
||
4438 | 13 |
'From Smalltalk/X, Version:3.5.2 on 22-mar-1999 at 12:47:13' ! |
14 |
||
1 | 15 |
Object subclass:#Signal |
1249 | 16 |
instanceVariableNames:'mayProceed notifierString nameClass message handlerBlock parent' |
4438 | 17 |
classVariableNames:'NoHandlerSignal GenericSignal ProceedErrorSignal |
18 |
WrongProceedabilitySignal' |
|
1249 | 19 |
poolDictionaries:'' |
20 |
category:'Kernel-Exceptions' |
|
1 | 21 |
! |
22 |
||
2207 | 23 |
!Signal class methodsFor:'documentation'! |
44 | 24 |
|
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
44 | 39 |
documentation |
40 |
" |
|
70 | 41 |
Signal and Exception provide a framework for exception handling. |
44 | 42 |
|
70 | 43 |
A Signal object is usually defined somewhere up in the calling chain |
44 |
and associated with some abnormal event. Many signals are also |
|
45 |
created at startup time and reused. |
|
44 | 46 |
|
70 | 47 |
When the event is raised (by Signal>>raise) the control will be either |
48 |
given to a debugger or - if a handler was defined - to the handler. |
|
49 |
The handler will get a description of what (and where) happened in an |
|
120 | 50 |
Exception object and can decide how to react on the situation (i.e. |
51 |
proceed, return or restart). |
|
70 | 52 |
|
53 |
There is also a companion class called SignalSet, which allows handling |
|
54 |
multiple signals with one handler (for example all arithmetic signals). |
|
55 |
And, finally there is a very special SignalSet which allows catching |
|
56 |
any signal (SignalSet>>anySignal). |
|
57 |
||
171 | 58 |
Since there is no official documentation on signal handling (i.e. none |
59 |
of the books describes it), this Signal implementation has been modeled |
|
60 |
after what some PD programs seem to expect and what alpha/beta testers told |
|
61 |
me it should look like. |
|
62 |
It may not be perfect and undergo minor changes. |
|
44 | 63 |
|
120 | 64 |
special: |
65 |
||
70 | 66 |
In addition to the nested catch & throw mechanism, signals can also be |
120 | 67 |
used when no such handler scope exists. To support this, signals can be |
68 |
assigned a handlerBlock, which gets evaluated with the exception as argument |
|
69 |
in case no handler was found (on the stack). |
|
70 | 70 |
|
171 | 71 |
If no handler was found (i.e. neither a handler context on the stack, nor |
72 |
a static handler block), the NoHandlerSignal will be raised instead, |
|
120 | 73 |
passing it the original exception in its exception-parameter. |
171 | 74 |
This NoHandlerSignal can be handled just like any other signal. |
75 |
(therefore, it is possible to catch any error by catching NoHandlerSignal. |
|
70 | 76 |
|
120 | 77 |
When the NoHandler signal is raised, and neither a handler-context, nor |
78 |
a handler block is defined for it, an emergencyHandler(-block) is evaluated. |
|
79 |
This block is either provided by the current process |
|
80 |
(see Process>>emergencySignalHandler) or as a global default by the Exception |
|
81 |
class (see Exception>>emergencyHandler). |
|
82 |
The default emergencyHandlerBlock (from Exception) will bring up a debugger. |
|
83 |
||
84 |
HandlerBlocks allow a global (if its the EmergencyHandler in Exception) |
|
85 |
or per-process signal handling to be added. Even to code which was never |
|
86 |
planned to handle signals. |
|
70 | 87 |
|
171 | 88 |
See samples in 'doc/coding' and actual raise code in Exception. |
1 | 89 |
|
1275 | 90 |
[Instance variables:] |
70 | 91 |
|
3157 | 92 |
mayProceed <Boolean> hint for the debugger - program may |
93 |
proceed (currently not honored by the |
|
94 |
debugger) |
|
1275 | 95 |
|
3157 | 96 |
notifierString <String> error message to be output |
70 | 97 |
|
3157 | 98 |
nameClass <Class> for the printOn-implementation; nameClass |
99 |
is the class, to which message (below) |
|
100 |
should be sent to create the receiver. |
|
1 | 101 |
|
3157 | 102 |
message <Symbol> for the printOn-implementation; message |
103 |
is the selector, which should be sent to |
|
104 |
nameClass (above) to create the receiver. |
|
1 | 105 |
|
3157 | 106 |
handlerBlock <Block> if nonNil, a 1-arg block to be |
107 |
evaluated when no handler context is |
|
108 |
found. The block gets the exception |
|
109 |
object as argument. This will play the role |
|
110 |
of an on-stack handler. |
|
1275 | 111 |
|
112 |
[Class variables:] |
|
70 | 113 |
|
3157 | 114 |
NoHandlerSignal <Signal> signal raised when no handler for a signal |
115 |
was found in raise. |
|
116 |
If this one is not handled either, |
|
117 |
Exceptions emergencyHandler will be evaluated |
|
118 |
instead (or a per-proces handler, if there |
|
119 |
is one). |
|
1292 | 120 |
|
121 |
[author:] |
|
3157 | 122 |
Claus Gittinger |
1292 | 123 |
|
1275 | 124 |
[see also:] |
3157 | 125 |
Exception |
126 |
SignalSet QuerySignal |
|
127 |
Object |
|
128 |
(``Exception handling and signals'': programming/exceptions.html) |
|
44 | 129 |
" |
130 |
! ! |
|
1 | 131 |
|
2207 | 132 |
!Signal class methodsFor:'initialization'! |
1 | 133 |
|
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
134 |
initGenericSignal |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
135 |
"setup the parent of all signals." |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
136 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
137 |
GenericSignal isNil ifTrue:[ |
4438 | 138 |
GenericSignal := self basicNew notifierString: 'unknown Signal with: '. |
139 |
GenericSignal nameClass:self message:#genericSignal. |
|
140 |
GenericSignal mayProceed:true. |
|
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
141 |
]. |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
142 |
|
4438 | 143 |
"Modified: / 8.10.1997 / 11:58:01 / cg" |
144 |
"Modified: / 27.2.1998 / 14:17:20 / stefan" |
|
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
145 |
! |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
146 |
|
159 | 147 |
initialize |
44 | 148 |
"setup the signal used to handle unhandled signals" |
149 |
||
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
150 |
GenericSignal isNil ifTrue:[ |
4438 | 151 |
self initGenericSignal. |
3010 | 152 |
]. |
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
153 |
|
3010 | 154 |
NoHandlerSignal isNil ifTrue:[ |
4438 | 155 |
NoHandlerSignal := ErrorSignal newSignal. |
156 |
NoHandlerSignal nameClass:self message:#noHandlerSignal. |
|
157 |
NoHandlerSignal notifierString:'unhandled exception'. |
|
158 |
ProceedErrorSignal := ErrorSignal newSignal. |
|
159 |
ProceedErrorSignal nameClass:self message:#proceedErrorSignal. |
|
160 |
ProceedErrorSignal notifierString:'handler tried to proceed from nonproceedable signal'. |
|
161 |
WrongProceedabilitySignal := ErrorSignal newSignal. |
|
162 |
WrongProceedabilitySignal nameClass:self message:#wrongProceedabilitySignal. |
|
163 |
WrongProceedabilitySignal notifierString:'attempt to raise a nonproceedable signal proceedable'. |
|
61 | 164 |
] |
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
165 |
|
4438 | 166 |
"Modified: / 8.10.1997 / 11:48:40 / cg" |
167 |
"Modified: / 27.2.1998 / 14:17:59 / stefan" |
|
1 | 168 |
! ! |
169 |
||
2207 | 170 |
!Signal class methodsFor:'instance creation'! |
1 | 171 |
|
172 |
new |
|
173 |
"return a new signal" |
|
174 |
||
4438 | 175 |
^ (self basicNew) notifierString:'signal'; mayProceed:true |
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
176 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
177 |
" |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
178 |
Signal new |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
179 |
" |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
180 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
181 |
"Modified: 8.10.1997 / 11:51:39 / cg" |
1 | 182 |
! ! |
183 |
||
2207 | 184 |
!Signal class methodsFor:'Signal constants'! |
1 | 185 |
|
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
186 |
genericSignal |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
187 |
"return the generic signal - thats the parent of all signals |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
188 |
in the system." |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
189 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
190 |
^ GenericSignal |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
191 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
192 |
"Created: 8.10.1997 / 11:46:28 / cg" |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
193 |
"Modified: 8.10.1997 / 11:47:08 / cg" |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
194 |
! |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
195 |
|
1 | 196 |
noHandlerSignal |
44 | 197 |
"return the signal used to handle unhandled signals" |
198 |
||
1 | 199 |
^ NoHandlerSignal |
4438 | 200 |
! |
201 |
||
202 |
proceedErrorSignal |
|
203 |
"return the signal used to indicate that a handler tried to |
|
204 |
proceed a signal marked as nonproceedable. |
|
205 |
The parameter for the exception raised by this signal is |
|
206 |
the exception which tried to proceed." |
|
207 |
||
208 |
^ ProceedErrorSignal |
|
209 |
||
210 |
"Modified: / 27.2.1998 / 14:22:13 / stefan" |
|
211 |
"Created: / 27.2.1998 / 14:36:15 / stefan" |
|
212 |
! |
|
213 |
||
214 |
wrongProceedabilitySignal |
|
215 |
"return the signal used to indicate that a signaler wants |
|
216 |
to raise a nonproceedable signal proceedable. |
|
217 |
proceed a signal marked as nonproceedable. |
|
218 |
The parameter for the exception raised by this signal is |
|
219 |
the unproceedable signal." |
|
220 |
||
221 |
^ WrongProceedabilitySignal |
|
222 |
||
223 |
"Created: / 27.2.1998 / 14:24:19 / stefan" |
|
1 | 224 |
! ! |
225 |
||
622 | 226 |
!Signal methodsFor:'accessing'! |
1 | 227 |
|
622 | 228 |
errorString |
4438 | 229 |
"return the notifier string. |
230 |
If the notifier string starts with space, prepend |
|
231 |
the parents notifier string" |
|
232 |
||
233 |
notifierString isNil ifTrue:[ |
|
234 |
^ parent errorString |
|
235 |
] ifFalse:[ |
|
236 |
(notifierString size > 0 |
|
237 |
and:[notifierString first == (Character space)]) ifTrue:[ |
|
238 |
^ parent errorString, notifierString |
|
239 |
] ifFalse:[ |
|
240 |
^ notifierString |
|
241 |
]. |
|
242 |
] |
|
243 |
||
244 |
" |
|
245 |
Object errorSignal errorString |
|
246 |
" |
|
247 |
||
248 |
"Modified: / 12.3.1998 / 15:04:41 / stefan" |
|
249 |
! |
|
250 |
||
251 |
errorStringExtra:extraString with:aParameter |
|
252 |
"used when raising with a given error string and/or parameter; |
|
253 |
if the errorString starts with a space, it is appended to the receivers |
|
254 |
notifier string; if it ends with a space, it is prepended. |
|
255 |
Otherwise, the extraString is returned. |
|
256 |
If no extraString is given, use the signals default errorString." |
|
257 |
||
258 |
|t| |
|
1 | 259 |
|
4438 | 260 |
(self inheritsFrom:Object userNotificationSignal) ifTrue:[ |
261 |
"/ all userNotifications pass the extraString unchanged. |
|
262 |
^ extraString |
|
263 |
]. |
|
264 |
||
265 |
extraString isNil ifTrue:[ |
|
266 |
t := self errorString |
|
267 |
] ifFalse:[ |
|
268 |
t := extraString. |
|
269 |
(extraString endsWith:Character space) ifTrue:[ |
|
270 |
t := extraString, self errorString |
|
271 |
] ifFalse:[ |
|
272 |
(extraString startsWith:Character space) ifTrue:[ |
|
273 |
t := self errorString, extraString |
|
274 |
] |
|
275 |
]. |
|
276 |
]. |
|
277 |
^ t. |
|
278 |
||
279 |
"/ aParameter isNil ifTrue:[ |
|
280 |
"/ ^ t |
|
281 |
"/ ]. |
|
282 |
"/ |
|
283 |
"/ (t startsWith:' ') ifTrue:[ |
|
284 |
"/ ^ aParameter printString , t |
|
285 |
"/ ]. |
|
286 |
"/ (t endsWith:' ') ifTrue:[ |
|
287 |
"/ ^ t , aParameter printString |
|
288 |
"/ ]. |
|
289 |
"/ ^ t |
|
290 |
||
291 |
"Modified: / 25.3.1997 / 12:12:37 / cg" |
|
292 |
"Created: / 12.3.1998 / 15:11:31 / stefan" |
|
622 | 293 |
! |
159 | 294 |
|
622 | 295 |
handlerBlock |
296 |
"return the handlerblock - if non-nil, this will be evaluated with the exception |
|
297 |
object as argument, if no #handle:do: context was found on the stack." |
|
298 |
||
299 |
^ handlerBlock |
|
1 | 300 |
! |
301 |
||
622 | 302 |
handlerBlock:aOneArgBlock |
303 |
"set the handlerblock - this will be evaluated with the exception |
|
304 |
object as argument, if no #handle:do: context was found on the stack." |
|
159 | 305 |
|
622 | 306 |
handlerBlock := aOneArgBlock |
307 |
! |
|
159 | 308 |
|
622 | 309 |
mayProceed |
310 |
"return the signals ability to proceed. |
|
311 |
This flag is (currently) not checked by the system; |
|
312 |
be prepared for changes here, to eventually have nonProceedable |
|
313 |
signals refuse to let you continue execution." |
|
1 | 314 |
|
622 | 315 |
^ mayProceed |
1 | 316 |
! |
317 |
||
318 |
mayProceed:aBoolean |
|
44 | 319 |
"set/clear the signals ability to proceed. |
419 | 320 |
This flag is (currently) not checked by the system; |
321 |
be prepared for changes here, to eventually have nonProceedable |
|
322 |
signals refuse to let you continue execution." |
|
1 | 323 |
|
324 |
mayProceed := aBoolean |
|
325 |
! |
|
326 |
||
622 | 327 |
nameClass:aClass message:aSelector |
328 |
"this sets the class & selector of a method which returns |
|
329 |
that signal - this is simply for documentation purposes - |
|
330 |
see Signal>>printOn: implementation. |
|
331 |
(took me a while to find that one out ;-)" |
|
419 | 332 |
|
622 | 333 |
nameClass := aClass. |
334 |
message := aSelector |
|
419 | 335 |
! |
336 |
||
622 | 337 |
notifierString |
338 |
"return the notifier string" |
|
159 | 339 |
|
622 | 340 |
^ notifierString |
341 |
! |
|
342 |
||
343 |
notifierString:aString |
|
344 |
"set the notifier string" |
|
345 |
||
346 |
notifierString := aString |
|
159 | 347 |
! |
348 |
||
349 |
parent |
|
350 |
"return the parent-signal of the receiver" |
|
351 |
||
352 |
^ parent |
|
353 |
! |
|
354 |
||
622 | 355 |
parent:aSignal |
356 |
"set the parent-signal of the receiver." |
|
357 |
||
358 |
parent := aSignal |
|
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
359 |
! |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
360 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
361 |
parent:aSignal mayProceed:aBoolean |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
362 |
"set the parent-signal and the mayProceed flag of the receiver." |
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
363 |
|
3010 | 364 |
parent := aSignal. |
365 |
mayProceed := aBoolean. |
|
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
366 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
367 |
"Modified: 8.10.1997 / 11:56:11 / cg" |
622 | 368 |
! ! |
369 |
||
370 |
!Signal methodsFor:'copying'! |
|
371 |
||
3348
37b0481be091
must catch #deppCopyUsing: instead of #deepCopy.
Claus Gittinger <cg@exept.de>
parents:
3280
diff
changeset
|
372 |
deepCopyUsing:aDictionary |
622 | 373 |
"raise an error - deepCopy is not allowed for signals" |
1 | 374 |
|
622 | 375 |
^ self deepCopyError |
3348
37b0481be091
must catch #deppCopyUsing: instead of #deepCopy.
Claus Gittinger <cg@exept.de>
parents:
3280
diff
changeset
|
376 |
|
37b0481be091
must catch #deppCopyUsing: instead of #deepCopy.
Claus Gittinger <cg@exept.de>
parents:
3280
diff
changeset
|
377 |
"Created: / 31.3.1998 / 15:43:01 / cg" |
622 | 378 |
! ! |
379 |
||
380 |
!Signal methodsFor:'instance creation'! |
|
1 | 381 |
|
622 | 382 |
newSignal |
383 |
"create a new signal, using the receiver as a prototype and |
|
384 |
setting the parent of the new signal to the receiver." |
|
1 | 385 |
|
3010 | 386 |
^ (self copy) parent:self |
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
387 |
|
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
388 |
"Modified: 8.10.1997 / 11:53:06 / cg" |
70 | 389 |
! |
390 |
||
622 | 391 |
newSignalMayProceed:aBoolean |
392 |
"create a new signal, using the receiver as a prototype and |
|
393 |
setting the parent of the new signal to the receiver." |
|
362 | 394 |
|
3010 | 395 |
^ (self copy) parent:self mayProceed:aBoolean |
70 | 396 |
|
3004
220af5090ec2
added the genericSignal for PP compatiblity
Claus Gittinger <cg@exept.de>
parents:
2893
diff
changeset
|
397 |
"Modified: 8.10.1997 / 11:57:13 / cg" |
1 | 398 |
! ! |
399 |
||
159 | 400 |
!Signal methodsFor:'printing'! |
401 |
||
402 |
printOn:aStream |
|
403 |
"append a printed representation of the receiver on aStream" |
|
404 |
||
405 |
nameClass notNil ifTrue:[ |
|
406 |
aStream nextPutAll:nameClass name. |
|
407 |
aStream space. |
|
408 |
aStream nextPutAll:message. |
|
409 |
^ self |
|
410 |
]. |
|
411 |
^ super printOn:aStream |
|
412 |
! ! |
|
413 |
||
622 | 414 |
!Signal methodsFor:'queries'! |
415 |
||
416 |
accepts:aSignal |
|
417 |
"return true, if the receiver accepts the argument, aSignal. |
|
418 |
(i.e. the receiver is aSignal or a parent of it). False otherwise." |
|
419 |
||
420 |
|s| |
|
421 |
||
422 |
aSignal isQuerySignal ifTrue:[^ false]. |
|
423 |
||
424 |
s := aSignal. |
|
425 |
[s notNil] whileTrue:[ |
|
4438 | 426 |
self == s ifTrue:[^ true]. |
427 |
s := s parent |
|
622 | 428 |
]. |
429 |
^ false |
|
4438 | 430 |
|
431 |
"Modified: / 22.3.1999 / 12:45:42 / stefan" |
|
622 | 432 |
! |
433 |
||
434 |
inheritsFrom:anotherSignal |
|
435 |
"return true, if the receiver is a child of anotherSignal |
|
436 |
(i.e. if handling anotherSignal also handles the receiver) |
|
437 |
This is almost the same as accepts, but returns false, if |
|
438 |
the receiver is identical to anotherSignal." |
|
439 |
||
440 |
self == anotherSignal ifTrue:[^ false]. |
|
441 |
^ anotherSignal accepts:self |
|
3521
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
442 |
|
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
443 |
"Modified: / 6.6.1998 / 20:37:47 / cg" |
622 | 444 |
! |
445 |
||
446 |
isHandled |
|
447 |
"return true, if there is a handler for the receiver signal. |
|
448 |
Raising an unhandled signal will usually lead into the debugger, |
|
2689 | 449 |
but can be caught globally by setting Exceptions EmergencyHandler." |
622 | 450 |
|
451 |
^ self isHandledIn:(thisContext sender). |
|
452 |
! |
|
453 |
||
454 |
isHandledIn:aContext |
|
455 |
"return true, if there is a handler for the receiver signal in the |
|
456 |
contextChain starting with aContext." |
|
457 |
||
3521
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
458 |
|con r| |
622 | 459 |
|
460 |
con := aContext. |
|
461 |
[con notNil] whileTrue:[ |
|
3521
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
462 |
con := con findNextContextWithSelector:#'handle:do:' or:#'handle:from:do:' or:nil. |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
463 |
con notNil ifTrue:[ |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
464 |
" |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
465 |
is this is a #handle:do: or a #handle:from:do: context |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
466 |
with self in it ? |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
467 |
" |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
468 |
|
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
469 |
((r := con receiver) notNil |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
470 |
and:[r accepts:self]) ifTrue:[ |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
471 |
"found a handler context" |
622 | 472 |
|
3521
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
473 |
^ true |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
474 |
] |
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
475 |
] |
622 | 476 |
]. |
477 |
^ false |
|
2207 | 478 |
|
3521
4d83701cfc28
care for nil receiver in #isHandledIn:
Claus Gittinger <cg@exept.de>
parents:
3348
diff
changeset
|
479 |
"Modified: / 6.6.1998 / 20:37:16 / cg" |
622 | 480 |
! |
481 |
||
482 |
isQuerySignal |
|
1249 | 483 |
"return true, if this is a querySignal - always return false here" |
484 |
||
622 | 485 |
^ false |
1249 | 486 |
|
487 |
"Modified: 22.4.1996 / 13:45:06 / cg" |
|
622 | 488 |
! |
489 |
||
490 |
isSignal |
|
491 |
"return true, if the receiver is some kind of signal; |
|
492 |
true returned here - the method is redefined from Object." |
|
493 |
||
494 |
^ true |
|
495 |
! ! |
|
496 |
||
497 |
!Signal methodsFor:'raising'! |
|
498 |
||
4438 | 499 |
newException |
500 |
"answer a new exception object for this signal. |
|
501 |
Subclasses may redefine this method" |
|
502 |
||
503 |
^ Exception signal:self |
|
504 |
parameter:nil |
|
505 |
errorString:nil |
|
506 |
suspendedContext:nil |
|
507 |
originator:nil |
|
508 |
||
509 |
"Created: / 26.2.1998 / 19:53:56 / stefan" |
|
510 |
"Modified: / 12.3.1998 / 15:14:19 / stefan" |
|
511 |
! |
|
512 |
||
513 |
newExceptionFrom:originator |
|
514 |
"answer a new exception object for this signal. |
|
515 |
Set the originator. |
|
516 |
Subclasses may redefine this method" |
|
517 |
||
518 |
^ Exception signal:self |
|
519 |
parameter:nil |
|
520 |
errorString:nil |
|
521 |
suspendedContext:nil |
|
522 |
originator:originator |
|
523 |
||
524 |
"Created: / 27.2.1998 / 09:17:00 / stefan" |
|
525 |
"Modified: / 12.3.1998 / 15:14:46 / stefan" |
|
526 |
! |
|
527 |
||
622 | 528 |
raise |
4438 | 529 |
"raise a signal nonproceedable. |
622 | 530 |
The signals notifierString is used as errorString." |
531 |
||
4438 | 532 |
^ self newException raise |
1326 | 533 |
|
4438 | 534 |
"Modified: / 2.5.1996 / 16:36:23 / cg" |
535 |
"Modified: / 5.3.1998 / 16:44:36 / stefan" |
|
622 | 536 |
! |
537 |
||
538 |
raiseErrorString:aString |
|
1326 | 539 |
"raise a signal nonproceedable. |
622 | 540 |
The argument is used as errorString." |
541 |
||
4438 | 542 |
^ (self newException errorString:aString) raise. |
1326 | 543 |
|
4438 | 544 |
"Modified: / 9.5.1996 / 15:17:59 / cg" |
545 |
"Modified: / 12.3.1998 / 15:15:22 / stefan" |
|
622 | 546 |
! |
547 |
||
1610 | 548 |
raiseErrorString:aString in:aContext |
549 |
"raise a signal nonproceedable. |
|
550 |
The argument is used as errorString. |
|
551 |
The additional context is passed as the context responsible for the raise, |
|
552 |
allowing a raise to mimicri the exception happened somewhere else." |
|
553 |
||
4438 | 554 |
^ (self newException |
555 |
signal:self |
|
556 |
parameter:nil |
|
557 |
errorString:aString |
|
558 |
suspendedContext:aContext |
|
559 |
originator:nil) raise. |
|
1610 | 560 |
|
4438 | 561 |
"Created: / 26.7.1996 / 16:42:32 / cg" |
562 |
"Modified: / 26.7.1996 / 16:42:47 / cg" |
|
563 |
"Modified: / 12.3.1998 / 15:43:43 / stefan" |
|
1610 | 564 |
! |
565 |
||
622 | 566 |
raiseFrom:something |
1326 | 567 |
"raise a signal nonproceedable. |
4438 | 568 |
The argument, something is passed both as parameter and originator." |
622 | 569 |
|
4438 | 570 |
^ ((self newExceptionFrom:something) parameter:something) raise |
1326 | 571 |
|
4438 | 572 |
"Modified: / 2.5.1996 / 16:36:38 / cg" |
573 |
"Modified: / 5.3.1998 / 16:49:55 / stefan" |
|
622 | 574 |
! |
575 |
||
576 |
raiseIn:aContext |
|
1326 | 577 |
"raise a signal nonproceedable. |
622 | 578 |
The signals notifierString is used as errorString. |
579 |
The additional context is passed as the context responsible for the raise, |
|
580 |
allowing a raise to mimicri the exception happened somewhere else." |
|
581 |
||
4438 | 582 |
^ (self newException suspendedContext:aContext) raise. |
1326 | 583 |
|
4438 | 584 |
"Modified: / 2.5.1996 / 16:36:44 / cg" |
585 |
"Modified: / 5.3.1998 / 16:50:21 / stefan" |
|
622 | 586 |
! |
587 |
||
588 |
raiseRequest |
|
1326 | 589 |
"raise a signal proceedable. |
622 | 590 |
The signals notifierString is used as errorString." |
591 |
||
4438 | 592 |
^ self newException raiseRequest. |
1326 | 593 |
|
4438 | 594 |
"Modified: / 2.5.1996 / 16:36:52 / cg" |
595 |
"Modified: / 5.3.1998 / 16:50:46 / stefan" |
|
622 | 596 |
! |
597 |
||
3103 | 598 |
raiseRequestFrom:something |
599 |
"raise a signal proceedable. |
|
4438 | 600 |
The argument, something is passed both as parameter and originator." |
3103 | 601 |
|
4438 | 602 |
^ ((self newExceptionFrom:something) parameter:something) raiseRequest. |
3103 | 603 |
|
4438 | 604 |
"Modified: / 2.5.1996 / 16:36:38 / cg" |
605 |
"Modified: / 5.3.1998 / 16:52:46 / stefan" |
|
3103 | 606 |
! |
607 |
||
622 | 608 |
raiseRequestWith:aParameter |
1326 | 609 |
"raise a signal proceedable. |
622 | 610 |
The signals notifierString is used as errorString." |
611 |
||
4438 | 612 |
^ (self newException parameter:aParameter) raiseRequest. |
1326 | 613 |
|
4438 | 614 |
"Modified: / 9.5.1996 / 15:13:20 / cg" |
615 |
"Modified: / 12.3.1998 / 15:16:57 / stefan" |
|
622 | 616 |
! |
617 |
||
618 |
raiseRequestWith:aParameter errorString:aString |
|
1326 | 619 |
"raise a signal proceedable. |
622 | 620 |
The argument, aString is used as errorString." |
621 |
||
4438 | 622 |
^ (self newException |
623 |
parameter:aParameter; |
|
624 |
errorString:aString |
|
625 |
) raiseRequest |
|
1326 | 626 |
|
4438 | 627 |
"Modified: / 9.5.1996 / 15:13:35 / cg" |
628 |
"Modified: / 12.3.1998 / 15:17:52 / stefan" |
|
622 | 629 |
! |
630 |
||
631 |
raiseRequestWith:aParameter errorString:aString in:aContext |
|
1326 | 632 |
"raise a signal proceedable. |
622 | 633 |
The argument, aString is used as errorString. |
1608 | 634 |
The additional context is passed as the context responsible for the raise, |
635 |
allowing a raise to mimicri the exception happened somewhere else." |
|
622 | 636 |
|
4438 | 637 |
^ (self newException |
638 |
parameter:aParameter; |
|
639 |
errorString:aString; |
|
640 |
suspendedContext:aContext) raiseRequest |
|
1326 | 641 |
|
4438 | 642 |
"Modified: / 26.7.1996 / 16:29:27 / cg" |
643 |
"Modified: / 12.3.1998 / 15:18:34 / stefan" |
|
622 | 644 |
! |
645 |
||
646 |
raiseRequestWith:aParameter in:aContext |
|
1326 | 647 |
"raise a signal proceedable. |
622 | 648 |
The signals notifierString is used as errorString. |
1608 | 649 |
The additional context is passed as the context responsible for the raise, |
650 |
allowing a raise to mimicri the exception happened somewhere else." |
|
622 | 651 |
|
4438 | 652 |
^ (self newException |
653 |
parameter:aParameter; |
|
654 |
suspendedContext:aContext) raiseRequest. |
|
1326 | 655 |
|
4438 | 656 |
"Modified: / 26.7.1996 / 16:29:33 / cg" |
657 |
"Modified: / 12.3.1998 / 15:18:55 / stefan" |
|
622 | 658 |
! |
659 |
||
660 |
raiseWith:aParameter |
|
1326 | 661 |
"raise a signal nonproceedable. |
4438 | 662 |
The argument, aParameter is passed as parameter." |
622 | 663 |
|
4438 | 664 |
^ (self newException parameter:aParameter) raise. |
1326 | 665 |
|
4438 | 666 |
"Modified: / 9.5.1996 / 15:14:24 / cg" |
667 |
"Modified: / 12.3.1998 / 15:19:11 / stefan" |
|
622 | 668 |
! |
669 |
||
670 |
raiseWith:aParameter errorString:aString |
|
4438 | 671 |
"raise a signal nonproceedable. |
622 | 672 |
The argument, aString is used as errorString, aParameter is passed |
673 |
as exception parameter." |
|
674 |
||
4438 | 675 |
^ (self newException |
676 |
parameter:aParameter; |
|
677 |
errorString:aString) raise. |
|
1326 | 678 |
|
4438 | 679 |
"Modified: / 9.5.1996 / 15:14:32 / cg" |
680 |
"Modified: / 12.3.1998 / 15:19:40 / stefan" |
|
1326 | 681 |
! |
682 |
||
683 |
raiseWith:aParameter errorString:aString in:aContext |
|
4438 | 684 |
"raise a signal nonproceedable. |
1326 | 685 |
The argument, aString is used as errorString, aParameter is passed |
1608 | 686 |
as exception parameter. |
687 |
The additional context is passed as the context responsible for the raise, |
|
688 |
allowing a raise to mimicri the exception happened somewhere else." |
|
689 |
||
1326 | 690 |
|
4438 | 691 |
^ (self newException |
692 |
parameter:aParameter; |
|
693 |
errorString:aString; |
|
694 |
suspendedContext:aContext) raise. |
|
1326 | 695 |
|
4438 | 696 |
"Created: / 2.5.1996 / 16:37:25 / cg" |
697 |
"Modified: / 26.7.1996 / 16:29:42 / cg" |
|
698 |
"Modified: / 12.3.1998 / 15:20:12 / stefan" |
|
3157 | 699 |
! |
700 |
||
701 |
raiseWith:aParameter in:aContext |
|
702 |
"raise a signal nonproceedable. |
|
4438 | 703 |
The argument, aParameter is passed as parameter." |
3157 | 704 |
|
4438 | 705 |
^ (self newException parameter:aParameter; |
706 |
suspendedContext:aContext) raise. |
|
3157 | 707 |
|
708 |
"Modified: / 9.5.1996 / 15:14:24 / cg" |
|
709 |
"Created: / 4.1.1998 / 14:38:15 / cg" |
|
4438 | 710 |
"Modified: / 12.3.1998 / 15:34:51 / stefan" |
622 | 711 |
! ! |
712 |
||
1 | 713 |
!Signal methodsFor:'save evaluation'! |
714 |
||
622 | 715 |
catch:aBlock |
716 |
"evaluate the argument, aBlock. |
|
717 |
If the receiver-signal is raised during evaluation, abort |
|
718 |
the evaluation and return true; otherwise return false. |
|
719 |
This is the catch & throw mechanism found in other languages, |
|
720 |
where the returned value indicates if an exception occured." |
|
721 |
||
722 |
|raiseOccurred| |
|
723 |
||
724 |
raiseOccurred := false. |
|
725 |
self handle:[:ex | raiseOccurred := true. ex return] do:aBlock. |
|
726 |
^ raiseOccurred |
|
727 |
||
728 |
" |
|
729 |
Object messageNotUnderstoodSignal catch:[ |
|
730 |
123 size open |
|
731 |
] |
|
732 |
" |
|
733 |
! |
|
734 |
||
1 | 735 |
handle:handleBlock do:aBlock |
736 |
"evaluate the argument, aBlock. |
|
737 |
If the receiver-signal is raised during evaluation, |
|
738 |
evaluate the handleBlock passing it an Exception argument. |
|
739 |
The handler may decide how to react to the signal by sending |
|
740 |
a corresponding message to the exception (see there). |
|
741 |
If the signal is not raised, return the value of evaluating |
|
742 |
aBlock." |
|
743 |
||
4438 | 744 |
^ aBlock value "the real logic is in Exception>>doRaise" |
95 | 745 |
|
746 |
" |
|
747 |
Object messageNotUnderstoodSignal handle:[:ex | |
|
4438 | 748 |
'oops' printNL. |
749 |
ex return |
|
95 | 750 |
] do:[ |
4438 | 751 |
123 size open |
95 | 752 |
] |
753 |
" |
|
120 | 754 |
|
95 | 755 |
" |
756 |
|num| |
|
757 |
||
758 |
num := 0. |
|
759 |
Number divisionByZeroSignal handle:[:ex | |
|
4438 | 760 |
'oops' printNL. |
761 |
ex return |
|
95 | 762 |
] do:[ |
4438 | 763 |
123 / num |
95 | 764 |
] |
765 |
" |
|
4438 | 766 |
|
767 |
"Modified: / 2.3.1998 / 14:28:26 / stefan" |
|
44 | 768 |
! |
769 |
||
362 | 770 |
handle:handleBlock from:anObject do:aBlock |
771 |
"evaluate the argument, aBlock. |
|
772 |
If the receiver-signal is raised during evaluation, |
|
773 |
and the exception originated from anObject, |
|
774 |
evaluate the handleBlock passing it an Exception argument. |
|
775 |
The handler may decide how to react to the signal by sending |
|
776 |
a corresponding message to the exception (see there). |
|
777 |
If the signal is not raised, return the value of evaluating |
|
778 |
aBlock." |
|
779 |
||
4438 | 780 |
^ aBlock value "the real logic is in Exception>>doRaise" |
362 | 781 |
|
782 |
" |
|
2689 | 783 |
the first open will be caught; the second not: |
362 | 784 |
|
785 |
|o1 o2| |
|
786 |
||
787 |
o1 := 123. |
|
788 |
o2 := nil. |
|
789 |
Object messageNotUnderstoodSignal |
|
4438 | 790 |
handle: |
791 |
[:ex | |
|
792 |
'oops' printNL. |
|
793 |
ex proceed |
|
794 |
] |
|
795 |
from:o1 |
|
796 |
do: |
|
797 |
[ |
|
798 |
o1 open. |
|
799 |
o2 open |
|
800 |
] |
|
362 | 801 |
" |
4438 | 802 |
|
803 |
"Modified: / 2.3.1998 / 14:28:05 / stefan" |
|
362 | 804 |
! |
805 |
||
419 | 806 |
ignoreIn:aBlock |
159 | 807 |
"evaluate the argument, aBlock. |
808 |
Ignore the receiver-signal during evaluation - i.e. simply |
|
419 | 809 |
continue. |
810 |
This makes only sense for some signals, such as UserInterrupt |
|
811 |
or AbortSignal, because continuing after an exception without any cleanup |
|
812 |
often leads to followup-errors." |
|
159 | 813 |
|
814 |
^ self handle:[:ex | ex proceed] do:aBlock. |
|
815 |
||
816 |
" |
|
419 | 817 |
Object messageNotUnderstoodSignal ignoreIn:[ |
159 | 818 |
123 size open |
61 | 819 |
] |
820 |
" |
|
1 | 821 |
! ! |
822 |
||
2207 | 823 |
!Signal class methodsFor:'documentation'! |
1249 | 824 |
|
825 |
version |
|
4438 | 826 |
^ '$Header: /cvs/stx/stx/libbasic/Signal.st,v 1.52 1999-07-23 07:54:42 stefan Exp $' |
1249 | 827 |
! ! |
622 | 828 |
Signal initialize! |