# Arithmethic overflow should not raise an exception

**URL:** https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006
**Category:** Crystal Contrib
**Created:** [April 26, 2020, 11:30am UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006 "2020-04-26T11:30:00Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)
#### Post date: [April 27, 2020, 8:38pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/21 "2020-04-27T20:38:37Z")

</div>

> [@IEatReturnValues4](#):
>
> However, the system doesn’t need to raise an exception

There wouldn’t _BE_ an exception if the system was properly handling this in the first place. I.e. validating the input.

> [@IEatReturnValues4](#):
>
> Crashing an application instead of defaulting a value to its MAX VALUE is not the best idea.

Id much rather have an exception than a silent bug. It would stop the items from being sold, inform the user there is a problem, and logging would capture something went wrong. The user doesn’t lose their items, and the application owner knows something didn’t work right.

Maybe for your use case defaulting to max would be the better option, but you can’t honestly say that is the behavior that should be used by default. It’s up to the application’s logic to handle these cases, i.e. the game itself.

EDIT: Does any language do that by default?

---

<div class="post-metadata">

### Author: ![paulcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulcsmith/32/301_2.png) [@paulcsmith](https://forum.crystal-lang.org/u/paulcsmith)
#### Post date: [April 27, 2020, 8:46pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/22 "2020-04-27T20:46:22Z")

</div>

Yes, silent bugs are bad. @IEatReturnValues4 Who’s to say I want the max value? Maybe I want the min value? Maybe I a completely different value. Silently setting to max makes that impossible, and dangerous.

In general @IEatReturnValues4 (really 1-4, not sure why 4 accounts?) has IMO not been living up to community standards in the code of conduct [https://github.com/crystal-lang/crystal/blob/master/CODE\_OF\_CONDUCT.md#our-standards](https://github.com/crystal-lang/crystal/blob/master/CODE_OF_CONDUCT.md#our-standards). @IEatReturnValues4 please reaad through those and try to be more welcoming of other opinions, more empathetic, and a bit kinder in your responses

---

<div class="post-metadata">

### Author: ![IEatReturnValues4](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IEatReturnValues4](https://forum.crystal-lang.org/u/IEatReturnValues4)
#### Post date: [April 27, 2020, 8:47pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/23 "2020-04-27T20:47:36Z")

</div>

> [@Blacksmoke16](#):
>
> There wouldn’t _BE_ an exception if the system was properly handling this in the first place. I.e. validating the input.

Not every system is handled like this. `experience`, and countless other `ivars` game types. In fact, any ivar that is an `Int`.

Your perspective / viewpoint on this issue would cause codebases to be littered with:

```auto
 if client.xxx < Int32::MAX

```

This is obviously not the correct solution. I hope the crystal dev loves writing colons!

---

<div class="post-metadata">

### Author: ![IEatReturnValues4](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IEatReturnValues4](https://forum.crystal-lang.org/u/IEatReturnValues4)
#### Post date: [April 27, 2020, 8:49pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/24 "2020-04-27T20:49:36Z")

</div>

> [@paulcsmith](#):
>
> Silently setting to max makes that impossible, and dangerous.

Silencing it to resort to the max value when it exceeded the max value is far less dangerous than crashing the program.

---

<div class="post-metadata">

### Author: ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)
#### Post date: [April 27, 2020, 8:52pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/25 "2020-04-27T20:52:53Z")

</div>

The rule that the amount of gold should max out on some value is clearly a domain-specific property.  
Thus it needs to be implement it in the application domain and not the language should be oriented for that very specific use case.  
When the addition operator raises, you can easily respond to that in you application code. An example would be this:

```auto
struct LimitedMoney
  def initialize(@value = 0)
  end

  def +(amount)
    new_value = @value + amount rescue Int32::MAX
    LimitedMoney.new(new_value)
  end
end

class Klass
  property gold = LimitedMoney.new
end

p = Klass.new
p.gold += Int32::MAX
p.gold += 1
pp p.gold

```

If the addition operator would just work in the way you expect, it would maybe fit your use case (as long as the application requirements don’t change) but it’s clearly not generically useable.

---

<div class="post-metadata">

### Author: ![IEatReturnValues4](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IEatReturnValues4](https://forum.crystal-lang.org/u/IEatReturnValues4)
#### Post date: [April 27, 2020, 8:53pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/26 "2020-04-27T20:53:33Z")

</div>

That is far too much code instead of just a one line if statement. Yikes, no thanks

---

<div class="post-metadata">

### Author: ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)
#### Post date: [April 27, 2020, 8:55pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/27 "2020-04-27T20:55:18Z")

</div>

If you need saturating operations you can do as I pointed [before](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/8)

```auto
lib LibIntrinsics
  fun sat_uadd8 = "llvm.uadd.sat.i8"(a : UInt8, b : UInt8) : UInt8
end

module Intrinsics
  def self.sadd(a : UInt8, b : UInt8) : UInt8
    LibIntrinsics.sat_uadd8(a, b)
  end
end

pp! a = 100_u8 # => 100
pp! a = Intrinsics.sadd(a, 100_u8) # => 200
pp! a = Intrinsics.sadd(a, 100_u8) # => 255

```

---

<div class="post-metadata">

### Author: ![watzon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/watzon/32/481_2.png) [@watzon](https://forum.crystal-lang.org/u/watzon)
#### Post date: [April 27, 2020, 9:08pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/28 "2020-04-27T21:08:55Z")

</div>

You have to realize 2 things:

First of all, this is the way overflows are handled in several languages. Crystal is not unique in this aspect. There are 3 ways to handle overflows, none of which are prefect, but only one of which is actually safe. The first way is that you wrap, this is what C does and it leads to unexpected behavior in many cases. The second is that you do what you’re requesting and just have a hard stopping point at the max value, this also can lead to unexpected behavior. And the third way is exactly how the Crystal devs have decided to do it, which is throwing an exception. No unexpected behavior, and exceptions are catchable, meaning if it overflows you can always catch the exception and set your integer to the max value yourself.

The second thing you need to realize is that this is a free and open source project in which hundreds of people have donated countless hours of development time and resources. It is impossible to make everyone happy, and so devs have to do what they see as being best for the language itself. Some decisions may end up requiring a few more lines of code in the end, but not everything should be about one-liners. **They are not important.**

Gotta say, you’ve not made a great first impression on many members of the community. Learn to listen more and talk less. Best advice I can give you.

---

<div class="post-metadata">

### Author: ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)
#### Post date: [April 27, 2020, 9:09pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/29 "2020-04-27T21:09:26Z")

</div>

> [@IEatReturnValues4](#):
>
> Crashing an application instead of defaulting a value to its MAX VALUE is not the best idea.

Defaulting to a max value would destroy my data if it overflowed and is the wrong approach for reliability. If my code has an instruction to do something i expect it to work, not silently discard my instructions.

Try this:

```auto
struct Int32
  def maybe_add(n)
    self + n
  rescue OverflowError
    MAX
  end
end

```

---

<div class="post-metadata">

### Author: ![IEatReturnValues4](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IEatReturnValues4](https://forum.crystal-lang.org/u/IEatReturnValues4)
#### Post date: [April 27, 2020, 9:11pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/30 "2020-04-27T21:11:43Z")

</div>

I’m not using `maybe_add` in the place of `+=`. Or, am I monkey patching `Int32`

Also, your post makes no sense. You can’t overflow something if it defaults to the max value.

@jhass Here is another post you can delete. You sure love to censor people, keep it up buddy.

---

<div class="post-metadata">

### Author: ![watzon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/watzon/32/481_2.png) [@watzon](https://forum.crystal-lang.org/u/watzon)
#### Post date: [April 27, 2020, 9:14pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/31 "2020-04-27T21:14:27Z")

</div>

Your conduct is way off, that’s why you’re being more or less muted in other topics. Toxicity like this isn’t welcome.

---

<div class="post-metadata">

### Author: ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)
#### Post date: [April 27, 2020, 9:14pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/32 "2020-04-27T21:14:27Z")

</div>

Many languages wrap around when overflowing. Does any language have the behavior he wants?

I think the request is extremely odd and is useless for anything except a video game counter. If I was storing a warehouse inventory counter I’d prefer an exception rather than losing track of how many items I have.

---

<div class="post-metadata">

### Author: ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)
#### Post date: [April 27, 2020, 9:14pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/33 "2020-04-27T21:14:38Z")

</div>

I’m kinda just curious now. Is there _ANY_ language that does what you’re suggesting?

---

<div class="post-metadata">

### Author: ![IEatReturnValues4](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IEatReturnValues4](https://forum.crystal-lang.org/u/IEatReturnValues4)
#### Post date: [April 27, 2020, 9:15pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/34 "2020-04-27T21:15:04Z")

</div>

Don’t reply to me then.

---

<div class="post-metadata">

### Author: ![watzon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/watzon/32/481_2.png) [@watzon](https://forum.crystal-lang.org/u/watzon)
#### Post date: [April 27, 2020, 9:15pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/35 "2020-04-27T21:15:16Z")

</div>

None that I’ve seen do, and it’s pretty simple to write a simple class that does exactly what he wants.

---

<div class="post-metadata">

### Author: ![watzon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/watzon/32/481_2.png) [@watzon](https://forum.crystal-lang.org/u/watzon)
#### Post date: [April 27, 2020, 9:15pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/36 "2020-04-27T21:15:42Z")

</div>

You’re writing in a public forum. Be respectful or be gone. It’s not that much to ask.

---

<div class="post-metadata">

### Author: ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)
#### Post date: [April 27, 2020, 9:17pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/37 "2020-04-27T21:17:20Z")

</div>

Don’t feed the trolls. He has at least 3 working example to do what he wants.

---

<div class="post-metadata">

### Author: ![IEatReturnValues4](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IEatReturnValues4](https://forum.crystal-lang.org/u/IEatReturnValues4)
#### Post date: [April 27, 2020, 9:17pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/38 "2020-04-27T21:17:28Z")

</div>

@watzon Blacksmoke said I’m a bad apple. Better tell him to be respectful to me as well then. Or, are you playing favorites? Just because you don’t agree with someone else and have differing viewpoints than yours?

---

<div class="post-metadata">

### Author: ![watzon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/watzon/32/481_2.png) [@watzon](https://forum.crystal-lang.org/u/watzon)
#### Post date: [April 27, 2020, 9:20pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/39 "2020-04-27T21:20:25Z")

</div>

You’ve been told several times how to do what you want, and why your idea is terrible from a language design perspective. You just don’t know how to listen. There is nothing more to be said. Case closed. Move on.

---

<div class="post-metadata">

### Author: ![IEatReturnValues4](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IEatReturnValues4](https://forum.crystal-lang.org/u/IEatReturnValues4)
#### Post date: [April 27, 2020, 9:20pm UTC](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006/40 "2020-04-27T21:20:38Z")

</div>

Saying Chromium is garbage and that it would waste development time **is not disrespectful or respectful**. It’s just an opinion.

Saying a person is “garbage” and insulting them, is toxicity.

There is a massive difference between the two. I hope you realize that.

> [@watzon](#):
>
> why your idea is terrible from a language design perspective.

Toxicity! Reported!

…oh wait

[Previous page](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006.md?page=1)

[Next page](https://forum.crystal-lang.org/t/arithmethic-overflow-should-not-raise-an-exception/2006.md?page=3)
