# Safer Integers

**URL:** <https://discourse.julialang.org/t/safer-integers/6447>\
**Category:** General Usage\
**Created:** [October 15, 2017, 12:44pm UTC](https://discourse.julialang.org/t/safer-integers/6447 "2017-10-15T12:44:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 15, 2017, 12:44pm UTC](https://discourse.julialang.org/t/safer-integers/6447/1 "2017-10-15T12:44:30Z")

</div>

Is it possible to throw an exception automatically when something like this happens:

```julia
print(typemax(UInt) + 2)

```

This returns `1`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 15, 2017, 12:54pm UTC](https://discourse.julialang.org/t/safer-integers/6447/2 "2017-10-15T12:54:53Z")

</div>

There is:

```julia
julia> Base.Checked.checked_add(typemax(UInt), 2)
ERROR: OverflowError()
Stacktrace:
 [1] checked_add at ./checked.jl:165 [inlined]
 [2] checked_add(::UInt64, ::Int64) at ./checked.jl:19

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 15, 2017, 12:59pm UTC](https://discourse.julialang.org/t/safer-integers/6447/3 "2017-10-15T12:59:06Z")

</div>

You could create a package that defines Integer types that use `Base.Checked` routines for all arithmetic operations. See [RoundingIntegers](https://github.com/JuliaMath/RoundingIntegers.jl) as a possible starting point.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 17, 2017, 1:52pm UTC](https://discourse.julialang.org/t/safer-integers/6447/4 "2017-10-17T13:52:11Z")

</div>

[SaferIntegers](https://github.com/JeffreySarnoff/SaferIntegers.jl)

untested – and the floor is open to suggestions while I am here today.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 17, 2017, 4:34pm UTC](https://discourse.julialang.org/t/safer-integers/6447/5 "2017-10-17T16:34:34Z")

</div>

wow, nice!! I think this will be useful.
