Why is ZeroDivide renewing?

In Pharo (and other dialects), the exclusion is ZeroDivide

renewed. What for?. For example, if you evaluate 1 / 0

and then continue, the answer is an error ZeroDivide

. Why is this? Can't we ZeroDivide

not renew?

EDIT : Let's take a closer look at this question.

The problem is, if an exception happens, then we get an exception ZeroDivide

. Thus, the only reason I can think of making this exception resumable is to include the following:

[a / b] on: ZeroDivide do: [:ex | ex resume: self anythingButTheQuotient],

      

right?

But this could also be written

[a / b] on: ZeroDevide do: [self anythingButTheQuotient]

      

without requiring the exception to resume.

Renewable exceptions make sense if there is "interesting" #defaultAction

. But it doesn't look like ZeroDivide

.

You might think that in many cases there is such code:

b = 0 ifTrue: [^0] ifFalse: [^a / b]

      

so why not use 0 as it #defaultAction

? This would make the above code simpler (in those cases) and would only require a dedicated handler in (perhaps) a few that should behave differently. However, this would be a very bad decision, because the default behavior would hide bugs that we all know will manifest the worst later.

+3


source to share


1 answer


Yes, it's amazing at first glance, but the ANSI standard says:

Zero division exceptions are resumed, so any message in this protocol that the signal for such an exception might end up returning to their originator.

The example you gave was trivial, but when setting up a handler multiple methods above, it is less trivial to resume the exception where it was signaled.



[self doSomethingComplex]
    on: ZeroDivide
    do:
        [:exception |
        "Handle zero divide as inf/nan as if performed in floating point arithmetic"
        exception resume: exception dividend asFloat / 0.0] 

      

In Squeak or Pharo, see the instance variable references signalContext

in the class Exception

. You will see that resume is your only way to return control to the signaling server.

+3


source







All Articles