author | Claus Gittinger <cg@exept.de> |
Wed, 26 Jun 2019 22:06:12 +0200 | |
changeset 24369 | e796791ea838 |
parent 24170 | 09200595d6d6 |
permissions | -rw-r--r-- |
24170 | 1 |
"{ Encoding: utf8 }" |
2 |
||
7585 | 3 |
" |
4 |
COPYRIGHT (c) 2001 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5947 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
20458 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
5947 | 18 |
DomainError subclass:#ZeroDivide |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
6218 | 22 |
category:'Kernel-Exceptions-Errors' |
5947 | 23 |
! |
24 |
||
6218 | 25 |
!ZeroDivide class methodsFor:'documentation'! |
26 |
||
7585 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 2001 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
6218 | 41 |
documentation |
42 |
" |
|
43 |
Raised when a division by zero is attempted. |
|
21863 | 44 |
|
45 |
To get the (in our oppinion: invalid) behavior of some programming languages such as JavaSript, |
|
46 |
which do not report division by zero exceptions, |
|
47 |
use the following code: |
|
48 |
||
49 |
ZeroDivide ignoreIn:[ x / 0.0 ] |
|
50 |
||
51 |
i.e. |
|
52 |
ZeroDivide ignoreIn:[ 1.0 / 0.0 ] |
|
53 |
returns +Inf, |
|
54 |
and |
|
55 |
ZeroDivide ignoreIn:[ -1.0 / 0.0 ] |
|
56 |
returns -Inf instead. |
|
6218 | 57 |
" |
7584 | 58 |
! |
59 |
||
60 |
examples |
|
61 |
" |
|
62 |
the following leads into a debugger: |
|
63 |
[exBegin] |
|
64 |
|divisor| |
|
65 |
||
66 |
divisor := 0. |
|
67 |
Transcript showCR: ( 5 / divisor ). |
|
68 |
[exEnd] |
|
69 |
||
70 |
||
10273
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
71 |
the following does NOT lead into a debugger: |
7584 | 72 |
[exBegin] |
73 |
|divisor| |
|
74 |
||
75 |
divisor := 0. |
|
76 |
[ |
|
77 |
Transcript showCR: ( 5 / divisor ). |
|
10273
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
78 |
] on:ZeroDivide do:[ |
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
79 |
Transcript flash. |
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
80 |
] |
7584 | 81 |
[exEnd] |
21867 | 82 |
|
83 |
the following suppresses the exception and results in NaN/Inf: |
|
84 |
[exBegin] |
|
85 |
|divisor| |
|
86 |
||
87 |
divisor := 0. |
|
88 |
Transcript showCR: (ZeroDivide ignoreIn:[ 5 / divisor ]) |
|
89 |
[exEnd] |
|
90 |
[exBegin] |
|
91 |
|divisor| |
|
92 |
||
93 |
divisor := 0. |
|
94 |
Transcript showCR: (ZeroDivide ignoreIn:[ -5 / divisor ]) |
|
95 |
[exEnd] |
|
96 |
[exBegin] |
|
97 |
|divisor| |
|
98 |
||
99 |
divisor := 0. |
|
100 |
Transcript showCR: (ZeroDivide ignoreIn:[ 0 / divisor ]) |
|
101 |
[exEnd] |
|
7584 | 102 |
" |
6218 | 103 |
! ! |
5947 | 104 |
|
6690
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
105 |
!ZeroDivide class methodsFor:'initialization'! |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
106 |
|
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
107 |
initialize |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
108 |
NotifierString := 'division by zero'. |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
109 |
! ! |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
110 |
|
24170 | 111 |
!ZeroDivide class methodsFor:'instance creation'! |
112 |
||
113 |
dividend:aNumber |
|
114 |
^ self new originator:aNumber |
|
115 |
||
116 |
" |
|
117 |
[ |
|
118 |
(ZeroDivide dividend:10) raise |
|
119 |
] on:ZeroDivide do:[:ex | |
|
120 |
self assert:ex dividend == 10 |
|
121 |
]. |
|
122 |
||
123 |
[ |
|
124 |
10 / 0 |
|
125 |
] on:ZeroDivide do:[:ex | |
|
126 |
self assert:ex dividend == 10 |
|
127 |
]. |
|
128 |
" |
|
129 |
||
130 |
"Created: / 27-05-2019 / 14:45:29 / Claus Gittinger" |
|
131 |
! ! |
|
132 |
||
5947 | 133 |
!ZeroDivide methodsFor:'accessing'! |
134 |
||
14717 | 135 |
defaultResumeValue |
21863 | 136 |
"return +/- infinity here, if ever proceeded" |
14717 | 137 |
|
21863 | 138 |
|poorReceiver| |
139 |
||
21867 | 140 |
poorReceiver := self dividend. |
141 |
poorReceiver isZero ifTrue:[ |
|
142 |
^ poorReceiver class NaN |
|
143 |
]. |
|
144 |
poorReceiver negative ifTrue:[ |
|
21863 | 145 |
^ poorReceiver class negativeInfinity |
146 |
]. |
|
147 |
^ poorReceiver class infinity |
|
14717 | 148 |
|
149 |
" |
|
150 |
|a b| |
|
151 |
||
152 |
a := 5. |
|
153 |
b := 0. |
|
154 |
ZeroDivide handle:[:ex | |
|
155 |
Transcript showCR:('division by zero - dividend was: ' , ex dividend printString). |
|
156 |
ex proceed |
|
157 |
] do:[ |
|
158 |
a / b |
|
159 |
] |
|
160 |
" |
|
161 |
||
162 |
" |
|
163 |
|a b| |
|
164 |
||
21863 | 165 |
a := -5.0. |
14717 | 166 |
b := 0.0. |
167 |
ZeroDivide handle:[:ex | |
|
168 |
Transcript showCR:('division by zero - dividend was: ' , ex dividend printString). |
|
169 |
ex proceed |
|
170 |
] do:[ |
|
171 |
a / b |
|
172 |
] |
|
173 |
" |
|
21863 | 174 |
|
21867 | 175 |
"Modified: / 20-06-2017 / 17:55:40 / cg" |
14717 | 176 |
! |
177 |
||
5947 | 178 |
dividend |
179 |
"Return the number that was being divided by zero." |
|
180 |
||
24170 | 181 |
^ originator |
182 |
"/ ^ parameter receiver |
|
5947 | 183 |
|
184 |
" |
|
185 |
|a b| |
|
186 |
||
187 |
a := 5. |
|
188 |
b := 0. |
|
189 |
Integer divisionByZeroSignal handle:[:ex | |
|
5969 | 190 |
Transcript showCR:('division by zero - dividend was: ' , ex dividend printString) |
5947 | 191 |
] do:[ |
192 |
a // b |
|
193 |
] |
|
194 |
" |
|
24170 | 195 |
|
196 |
"Modified: / 27-05-2019 / 14:49:41 / Claus Gittinger" |
|
5947 | 197 |
! ! |
198 |
||
199 |
!ZeroDivide class methodsFor:'documentation'! |
|
200 |
||
201 |
version |
|
20458 | 202 |
^ '$Header$' |
21867 | 203 |
! |
204 |
||
205 |
version_CVS |
|
206 |
^ '$Header$' |
|
5947 | 207 |
! ! |
7584 | 208 |
|
14717 | 209 |
|
6690
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
210 |
ZeroDivide initialize! |