ZeroDivide.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 24170 09200595d6d6
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2001 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

"{ NameSpace: Smalltalk }"

DomainError subclass:#ZeroDivide
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Kernel-Exceptions-Errors'
!

!ZeroDivide class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2001 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Raised when a division by zero is attempted.

    To get the (in our oppinion: invalid) behavior of some programming languages such as JavaSript,
    which do not report division by zero exceptions,
    use the following code:
    
        ZeroDivide ignoreIn:[ x / 0.0 ] 

    i.e.    
        ZeroDivide ignoreIn:[ 1.0 / 0.0 ] 
    returns +Inf,   
    and
        ZeroDivide ignoreIn:[ -1.0 / 0.0 ] 
    returns -Inf instead.    
"
!

examples
"
    the following leads into a debugger:
                                                                    [exBegin]
        |divisor|

        divisor := 0.
        Transcript showCR: ( 5 / divisor ).
                                                                    [exEnd]


    the following does NOT lead into a debugger:
                                                                    [exBegin]
        |divisor|

        divisor := 0.
        [
            Transcript showCR: ( 5 / divisor ).
        ] on:ZeroDivide do:[
            Transcript flash.
        ]
                                                                    [exEnd]

    the following suppresses the exception and results in NaN/Inf:
                                                                    [exBegin]
        |divisor|

        divisor := 0.
        Transcript showCR: (ZeroDivide ignoreIn:[ 5 / divisor ])
                                                                    [exEnd]
                                                                    [exBegin]
        |divisor|

        divisor := 0.
        Transcript showCR: (ZeroDivide ignoreIn:[ -5 / divisor ])
                                                                    [exEnd]
                                                                    [exBegin]
        |divisor|

        divisor := 0.
        Transcript showCR: (ZeroDivide ignoreIn:[ 0 / divisor ])
                                                                    [exEnd]
"
! !

!ZeroDivide class methodsFor:'initialization'!

initialize
    NotifierString := 'division by zero'.
! !

!ZeroDivide class methodsFor:'instance creation'!

dividend:aNumber
    ^ self new originator:aNumber

    "
     [
         (ZeroDivide dividend:10) raise
     ] on:ZeroDivide do:[:ex |
         self assert:ex dividend == 10
     ].    

     [
         10 / 0
     ] on:ZeroDivide do:[:ex |
         self assert:ex dividend == 10
     ].    
    "

    "Created: / 27-05-2019 / 14:45:29 / Claus Gittinger"
! !

!ZeroDivide methodsFor:'accessing'!

defaultResumeValue
    "return +/- infinity here, if ever proceeded"

    |poorReceiver|
    
    poorReceiver := self dividend.
    poorReceiver isZero ifTrue:[
        ^ poorReceiver class NaN
    ].    
    poorReceiver negative ifTrue:[
        ^ poorReceiver class negativeInfinity
    ].    
    ^ poorReceiver class infinity

    "
     |a b|

     a := 5.
     b := 0.
     ZeroDivide handle:[:ex |
        Transcript showCR:('division by zero - dividend was: ' , ex dividend printString).
        ex proceed
     ] do:[
        a / b
     ]            
    "

    "
     |a b|

     a := -5.0.
     b := 0.0.
     ZeroDivide handle:[:ex |
        Transcript showCR:('division by zero - dividend was: ' , ex dividend printString).
        ex proceed
     ] do:[
        a / b
     ]            
    "

    "Modified: / 20-06-2017 / 17:55:40 / cg"
!

dividend
    "Return the number that was being divided by zero."

    ^ originator
"/    ^ parameter receiver

    "
     |a b|

     a := 5.
     b := 0.
     Integer divisionByZeroSignal handle:[:ex |
        Transcript showCR:('division by zero - dividend was: ' , ex dividend printString)
     ] do:[
        a // b
     ]
    "

    "Modified: / 27-05-2019 / 14:49:41 / Claus Gittinger"
! !

!ZeroDivide class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !


ZeroDivide initialize!