class: ArithmeticValue
authorStefan Vogel <sv@exept.de>
Wed, 17 Dec 2014 09:08:47 +0100
changeset 17223 db522b3aff6f
parent 17222 9ff268844b9c
child 17224 43a8ca39cc90
class: ArithmeticValue added: #raise:receiver:selector:arguments:errorString: - moved from Number changed: #even
ArithmeticValue.st
--- a/ArithmeticValue.st	Tue Dec 16 13:21:57 2014 +0100
+++ b/ArithmeticValue.st	Wed Dec 17 09:08:47 2014 +0100
@@ -193,6 +193,44 @@
     self subclassResponsibility
 ! !
 
+!ArithmeticValue class methodsFor:'error reporting'!
+
+raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arguments:argArray errorString:text 
+    "ST-80 compatible signal raising. Provided for PD numeric classes.
+     aSignalSymbolOrErrorClass is either an Error-subclass, or
+     the selector which is sent to myself, to retrieve the Exception class / Signal."
+
+    <context: #return>
+
+    |msg signalOrException|
+
+    msg := MessageSend
+                receiver:someNumber
+                selector:sel
+                arguments:argArray.
+
+    aSignalSymbolOrErrorClass isSymbol ifTrue:[
+        signalOrException := self perform:aSignalSymbolOrErrorClass.
+    ] ifFalse:[
+        signalOrException := aSignalSymbolOrErrorClass.    "/ assume its an Error-Subclass
+    ].
+
+    ^ signalOrException
+         raiseRequestWith:msg 
+         errorString:text 
+         in:thisContext sender
+
+    "
+     Number 
+        raise:#domainErrorSignal
+        receiver:1.0
+        selector:#foo 
+        errorString:'foo bar test'
+    "
+
+    "Modified: / 16.11.2001 / 14:12:09 / cg"
+! !
+
 !ArithmeticValue class methodsFor:'queries'!
 
 isAbstract
@@ -1235,10 +1273,22 @@
 !
 
 even
-    "return true if the receiver is divisible by 2"
+    "return true if the receiver is divisible by 2.
+     Only defined for whole-numbers."
 
-    ^ self error:'non integer receiver'
-    "/ ^ self truncated asInteger even
+    ^ self class
+        raise:DomainError
+        receiver:self
+        selector:#even
+        arguments:#()
+        errorString:'Receiver must be a whole-number'
+
+    "/ No, this is bad: ^ self truncated asInteger even
+
+    "
+        2.4 even
+        2.0 even
+    "
 !
 
 isComplex
@@ -1426,11 +1476,11 @@
 !ArithmeticValue class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.97 2014-12-16 12:21:57 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.98 2014-12-17 08:08:47 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.97 2014-12-16 12:21:57 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.98 2014-12-17 08:08:47 stefan Exp $'
 ! !