# InexactError vs OverflowError

**URL:** <https://discourse.julialang.org/t/inexacterror-vs-overflowerror/127108>\
**Category:** General Usage\
**Tags:** numbers, exception, inexacterror, integer-overflow, arithmetic\
**Created:** [March 18, 2025, 5:38pm UTC](https://discourse.julialang.org/t/inexacterror-vs-overflowerror/127108 "2025-03-18T17:38:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 18, 2025, 5:38pm UTC](https://discourse.julialang.org/t/inexacterror-vs-overflowerror/127108/1 "2025-03-18T17:38:00Z")

</div>

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:

- 

```julia
Int(widen(a) * b) # may throw `InexactError` if the `widen(Int)` value doesn't fit into `Int`

```

- 

```julia
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` and `OverflowError`, in general?
- Does a legitimate motivation for using `OverflowError` even exist? Should we just always use `InexactError` instead?
- Which of the three implementations above would you use? Or maybe you’d use some other implementation?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 18, 2025, 6:33pm UTC](https://discourse.julialang.org/t/inexacterror-vs-overflowerror/127108/2 "2025-03-18T18:33:18Z")

</div>

In Base at least, I believe that InexactError is only used for conversions, which feels different from this. Then again I’m not clear that it’s an overflow in your case either… need a little more detail.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 18, 2025, 6:40pm UTC](https://discourse.julialang.org/t/inexacterror-vs-overflowerror/127108/3 "2025-03-18T18:40:26Z")

</div>

Ended up going with `OverflowError`, this is the PR:

- [throw `OverflowError` when operation result is not representable (!301) · Merge requests · Neven Sajko / TypeDomainNaturalNumbers.jl · GitLab](https://gitlab.com/nsajko/TypeDomainNaturalNumbers.jl/-/merge_requests/301)

> [@StefanKarpinski](#):
>
> I believe that InexactError is only used for conversions

Thanks, that makes sense, and I guess it justifies the choice I went with.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 18, 2025, 7:05pm UTC](https://discourse.julialang.org/t/inexacterror-vs-overflowerror/127108/4 "2025-03-18T19:05:55Z")

</div>

I don’t know if this was ever the intent, but my expectation as a user is that I could get around an `OverflowError` in _an operation_ by using a similar type with more bits, e.g. `Int64` gets me more integer values than `Int32`, but an `InexactError` is a fundamental inability of the type to _convert_ another type’s values e.g. `Int64(1im), Int128(3.5), Int8(300)`. It’s a bit of a misnomer because it’s not about losing precision in rounding, `Float32(1.2321976518237652)` silently does that. There’s probably some proper mathematical way of describing the values’ subsetting that distinguishes these.
