author | Claus Gittinger <cg@exept.de> |
Tue, 02 Apr 2019 14:11:50 +0200 | |
changeset 24038 | 917ed6428d22 |
parent 21867 | 35e2dfcb1e46 |
child 24170 | 09200595d6d6 |
permissions | -rw-r--r-- |
7585 | 1 |
" |
2 |
COPYRIGHT (c) 2001 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 |
" |
|
5947 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
20458 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
5947 | 16 |
DomainError subclass:#ZeroDivide |
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
6218 | 20 |
category:'Kernel-Exceptions-Errors' |
5947 | 21 |
! |
22 |
||
6218 | 23 |
!ZeroDivide class methodsFor:'documentation'! |
24 |
||
7585 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 2001 by eXept Software AG |
|
28 |
All Rights Reserved |
|
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 |
||
6218 | 39 |
documentation |
40 |
" |
|
41 |
Raised when a division by zero is attempted. |
|
21863 | 42 |
|
43 |
To get the (in our oppinion: invalid) behavior of some programming languages such as JavaSript, |
|
44 |
which do not report division by zero exceptions, |
|
45 |
use the following code: |
|
46 |
||
47 |
ZeroDivide ignoreIn:[ x / 0.0 ] |
|
48 |
||
49 |
i.e. |
|
50 |
ZeroDivide ignoreIn:[ 1.0 / 0.0 ] |
|
51 |
returns +Inf, |
|
52 |
and |
|
53 |
ZeroDivide ignoreIn:[ -1.0 / 0.0 ] |
|
54 |
returns -Inf instead. |
|
6218 | 55 |
" |
7584 | 56 |
! |
57 |
||
58 |
examples |
|
59 |
" |
|
60 |
the following leads into a debugger: |
|
61 |
[exBegin] |
|
62 |
|divisor| |
|
63 |
||
64 |
divisor := 0. |
|
65 |
Transcript showCR: ( 5 / divisor ). |
|
66 |
[exEnd] |
|
67 |
||
68 |
||
10273
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
69 |
the following does NOT lead into a debugger: |
7584 | 70 |
[exBegin] |
71 |
|divisor| |
|
72 |
||
73 |
divisor := 0. |
|
74 |
[ |
|
75 |
Transcript showCR: ( 5 / divisor ). |
|
10273
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
76 |
] on:ZeroDivide do:[ |
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
77 |
Transcript flash. |
84eddf61acc5
Fix spelling in example comment
Stefan Vogel <sv@exept.de>
parents:
7585
diff
changeset
|
78 |
] |
7584 | 79 |
[exEnd] |
21867 | 80 |
|
81 |
the following suppresses the exception and results in NaN/Inf: |
|
82 |
[exBegin] |
|
83 |
|divisor| |
|
84 |
||
85 |
divisor := 0. |
|
86 |
Transcript showCR: (ZeroDivide ignoreIn:[ 5 / divisor ]) |
|
87 |
[exEnd] |
|
88 |
[exBegin] |
|
89 |
|divisor| |
|
90 |
||
91 |
divisor := 0. |
|
92 |
Transcript showCR: (ZeroDivide ignoreIn:[ -5 / divisor ]) |
|
93 |
[exEnd] |
|
94 |
[exBegin] |
|
95 |
|divisor| |
|
96 |
||
97 |
divisor := 0. |
|
98 |
Transcript showCR: (ZeroDivide ignoreIn:[ 0 / divisor ]) |
|
99 |
[exEnd] |
|
7584 | 100 |
" |
6218 | 101 |
! ! |
5947 | 102 |
|
6690
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
103 |
!ZeroDivide class methodsFor:'initialization'! |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
104 |
|
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
105 |
initialize |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
106 |
NotifierString := 'division by zero'. |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
107 |
! ! |
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
108 |
|
5947 | 109 |
!ZeroDivide methodsFor:'accessing'! |
110 |
||
14717 | 111 |
defaultResumeValue |
21863 | 112 |
"return +/- infinity here, if ever proceeded" |
14717 | 113 |
|
21863 | 114 |
|poorReceiver| |
115 |
||
21867 | 116 |
poorReceiver := self dividend. |
117 |
poorReceiver isZero ifTrue:[ |
|
118 |
^ poorReceiver class NaN |
|
119 |
]. |
|
120 |
poorReceiver negative ifTrue:[ |
|
21863 | 121 |
^ poorReceiver class negativeInfinity |
122 |
]. |
|
123 |
^ poorReceiver class infinity |
|
14717 | 124 |
|
125 |
" |
|
126 |
|a b| |
|
127 |
||
128 |
a := 5. |
|
129 |
b := 0. |
|
130 |
ZeroDivide handle:[:ex | |
|
131 |
Transcript showCR:('division by zero - dividend was: ' , ex dividend printString). |
|
132 |
ex proceed |
|
133 |
] do:[ |
|
134 |
a / b |
|
135 |
] |
|
136 |
" |
|
137 |
||
138 |
" |
|
139 |
|a b| |
|
140 |
||
21863 | 141 |
a := -5.0. |
14717 | 142 |
b := 0.0. |
143 |
ZeroDivide handle:[:ex | |
|
144 |
Transcript showCR:('division by zero - dividend was: ' , ex dividend printString). |
|
145 |
ex proceed |
|
146 |
] do:[ |
|
147 |
a / b |
|
148 |
] |
|
149 |
" |
|
21863 | 150 |
|
21867 | 151 |
"Modified: / 20-06-2017 / 17:55:40 / cg" |
14717 | 152 |
! |
153 |
||
5947 | 154 |
dividend |
155 |
"Return the number that was being divided by zero." |
|
156 |
||
5969 | 157 |
^ parameter receiver |
5947 | 158 |
|
159 |
" |
|
160 |
|a b| |
|
161 |
||
162 |
a := 5. |
|
163 |
b := 0. |
|
164 |
Integer divisionByZeroSignal handle:[:ex | |
|
5969 | 165 |
Transcript showCR:('division by zero - dividend was: ' , ex dividend printString) |
5947 | 166 |
] do:[ |
167 |
a // b |
|
168 |
] |
|
169 |
" |
|
170 |
! ! |
|
171 |
||
172 |
!ZeroDivide class methodsFor:'documentation'! |
|
173 |
||
174 |
version |
|
20458 | 175 |
^ '$Header$' |
21867 | 176 |
! |
177 |
||
178 |
version_CVS |
|
179 |
^ '$Header$' |
|
5947 | 180 |
! ! |
7584 | 181 |
|
14717 | 182 |
|
6690
07ef28245da8
notifier now initialized here
Claus Gittinger <cg@exept.de>
parents:
6218
diff
changeset
|
183 |
ZeroDivide initialize! |