The easiest way is to simply signal a common error:
someVariable: anInteger
anInteger isInteger
ifFalse: [self error: 'must be an Integer'].
someVariable := anInteger
Now, if you want to report a specific error, do the following:
- Subclass
Errorlet's sayNonIntegerArgument Write your code as follows
someVariable: anInteger
anInteger isInteger
ifFalse: [NonIntegerArgument signal: 'must be an Integer'].
someVariable := anInteger
To handle this exception, do the following
[myObject someVariable: self value]
on: NonIntegerArgument
do: [:ex | self handleException: ex]
Please note that your exception may provide additional information, such as the actual argument that was sent. To do this, add the instance variable to the class NonIntegerArgument, namely argument. Add a getter and setter for it. Then
NonIntegerArgument class >>
^self new
argument: anObject;
signal: aString
and use it that way
someVariable: anInteger
anInteger isInteger
ifFalse: [
NonIntegerArgument
signal: 'must be an Integer'
argument: anInteger].
someVariable := anInteger
Now the variable exwill be able to respond with an argumentoffensive message.
source
share