I have a number type which throws when the result of a binary operation like +
or *
is not representable in this type. I think InexactError
is the right choice of exception, while OverflowError
seems like a bad choice because it seems like exposing implementation details. Or should OverflowError
perhaps be considered as a more specific InexactError
?
My type basically wraps Int
, so the two simplest implementations could look like this, assuming we’re implementing the binary *
method:
-
Int(widen(a) * b) # may throw `InexactError` if the `widen(Int)` value doesn't fit into `Int`
-
Base.checked_mul(a, b) # more efficient (?), but may throw `OverflowError` in case of overflow
- a third option could be to use
Base.mul_with_overflow(a, b)
, then throw and choose exception type explicitly
Questions:
- How to choose between
InexactError
andOverflowError
, in general? - Does a legitimate motivation for using
OverflowError
even exist? Should we just always useInexactError
instead? - Which of the three implementations above would you use? Or maybe you’d use some other implementation?