|
1 " |
|
2 COPYRIGHT (c) 2002 by eXept Software AG |
|
3 All Rights Reserved |
|
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 "{ Package: 'stx:libbasic' }" |
|
14 |
|
15 Notification subclass:#Query |
|
16 instanceVariableNames:'' |
|
17 classVariableNames:'' |
|
18 poolDictionaries:'' |
|
19 category:'Kernel-Exceptions' |
|
20 ! |
|
21 |
|
22 !Query class methodsFor:'documentation'! |
|
23 |
|
24 copyright |
|
25 " |
|
26 COPYRIGHT (c) 2002 by eXept Software AG |
|
27 All Rights Reserved |
|
28 |
|
29 This software is furnished under a license and may be used |
|
30 only in accordance with the terms of that license and with the |
|
31 inclusion of the above copyright notice. This software may not |
|
32 be provided or otherwise made available to, or used by, any |
|
33 other person. No title to or ownership of the software is |
|
34 hereby transferred. |
|
35 " |
|
36 ! |
|
37 |
|
38 documentation |
|
39 " |
|
40 Query is an abstract superclass for queries. |
|
41 |
|
42 A query is an exception which by default proceeds if unhandled and returns |
|
43 a default value. |
|
44 As a class based reimplementation, it replaces and obsoletes the old |
|
45 QuerySignal instance based mechanism. |
|
46 |
|
47 Note: |
|
48 Query does not add/refine any functionality from its superclass. |
|
49 It exists for the more descriptive class name only. |
|
50 |
|
51 [author:] |
|
52 Claus Gittinger |
|
53 |
|
54 [see also:] |
|
55 Warning Signal QuerySignal |
|
56 " |
|
57 ! |
|
58 |
|
59 examples |
|
60 " |
|
61 [exBegin] |
|
62 Query answer:'hello' |
|
63 do:[ |
|
64 Transcript showCR:(Query query) |
|
65 ] |
|
66 [exEnd] |
|
67 |
|
68 |
|
69 an up-query from a deeply nested operation to a higher level: |
|
70 the example below demonstrates that a Query is not an Error |
|
71 (i.e. the Error-handler does not interfere with Queries) |
|
72 [exBegin] |
|
73 |zero| |
|
74 |
|
75 zero := 0. |
|
76 Query handle:[:ex | |
|
77 Transcript showCR:'query'. |
|
78 ex proceedWith:true |
|
79 ] do:[ |
|
80 'nesting'. |
|
81 [ |
|
82 [ |
|
83 Error handle:[:ex | |
|
84 Transcript showCR:'some error: ' , ex errorString. |
|
85 ex proceed |
|
86 ] do:[ |
|
87 [ |
|
88 1 // zero. 'an error which is caught in the handler'. |
|
89 (Query query) == true ifTrue:[ |
|
90 Transcript showCR:'query says: ok'. |
|
91 ] ifFalse:[ |
|
92 Transcript showCR:'query says: no' |
|
93 ] |
|
94 ] value |
|
95 ] |
|
96 ] value |
|
97 ] value |
|
98 ] |
|
99 [exEnd] |
|
100 for lazy typists, a more compact interface for query-answerign |
|
101 is also provided (which is also easier to read): |
|
102 [exBegin] |
|
103 Query answer:true do:[ |
|
104 'nesting'. |
|
105 [ |
|
106 [ |
|
107 (Query query) == true ifTrue:[ |
|
108 Transcript showCR:'query says: ok'. |
|
109 ] ifFalse:[ |
|
110 Transcript showCR:'query says: no' |
|
111 ] |
|
112 ] value |
|
113 ] value |
|
114 ] |
|
115 [exEnd] |
|
116 an up-query from a deeply nested operation, for which there |
|
117 is no handler: |
|
118 (notice, this would not work with normal signals, which would raise |
|
119 another unhandled exception-exception; |
|
120 also notice the == check #raise's return value being true, |
|
121 instead of a simple ifTrue; this handles a nil-value from |
|
122 the unhandled query) |
|
123 [exBegin] |
|
124 |zero| |
|
125 |
|
126 zero := 0. |
|
127 [ |
|
128 'nesting'. |
|
129 [ |
|
130 [ |
|
131 Error handle:[:ex | |
|
132 Transcript showCR:'some error: ' , ex errorString. |
|
133 ex proceed |
|
134 ] do:[ |
|
135 [ |
|
136 1 // zero. 'an error which is caught in the handler'. |
|
137 (Query raise) == true ifTrue:[ |
|
138 Transcript showCR:'query says: ok'. |
|
139 ] ifFalse:[ |
|
140 Transcript showCR:'query says: no' |
|
141 ] |
|
142 ] value |
|
143 ] |
|
144 ] value |
|
145 ] value |
|
146 ] value |
|
147 [exEnd] |
|
148 counter-example, just to show that things would not work this way |
|
149 with regular signals: |
|
150 [exBegin] |
|
151 |signal| |
|
152 |
|
153 signal := Signal new. |
|
154 'nesting deeply'. |
|
155 [ |
|
156 [ |
|
157 [ |
|
158 [ |
|
159 [ |
|
160 (signal raise) == true ifTrue:[ |
|
161 Transcript showCR:'query says: ok'. |
|
162 ] ifFalse:[ |
|
163 Transcript showCR:'query says: no' |
|
164 ] |
|
165 ] value |
|
166 ] value |
|
167 ] value |
|
168 ] value |
|
169 ] value |
|
170 [exEnd] |
|
171 |
|
172 except, by handling the unhandled exception |
|
173 (but we think, that querySignals are easier to use and |
|
174 better document the intent): |
|
175 [exBegin] |
|
176 |signal| |
|
177 |
|
178 signal := Signal new. |
|
179 'nesting deeply'. |
|
180 [ |
|
181 [ |
|
182 [ |
|
183 [ |
|
184 [ |
|
185 Signal noHandlerSignal handle:[:ex | |
|
186 ex proceedWith:nil |
|
187 ] do:[ |
|
188 (signal raise) == true ifTrue:[ |
|
189 Transcript showCR:'query says: ok'. |
|
190 ] ifFalse:[ |
|
191 Transcript showCR:'query says: no' |
|
192 ] |
|
193 ] |
|
194 ] value |
|
195 ] value |
|
196 ] value |
|
197 ] value |
|
198 ] value |
|
199 [exEnd] |
|
200 " |
|
201 ! ! |
|
202 |
|
203 !Query class methodsFor:'initialization'! |
|
204 |
|
205 initialize |
|
206 |
|
207 NotifierString := 'Query'. |
|
208 |
|
209 " |
|
210 self initialize |
|
211 " |
|
212 ! ! |
|
213 |
|
214 Query initialize! |