Catch div 0

Johannes Loher johannes.loher at fg4f.de
Sat Jul 20 21:11:34 UTC 2019


Am 20.07.19 um 19:31 schrieb mogu:
> Should d make div 0 a runtime exception? Or as an optional feature?
> I never want my program crashes only beause of reading a wrong number
> from runtime configurations.
> 

I suggest you take a look at std.experimental.checkedint
(https://dlang.org/phobos/std_experimental_checkedint.html). It allows
you to specify what exact behavior you want to have on a very fine
granular level and also defines a few very useful presets, e.g. `Throw`,
which looks very much like you want:

```
import std.exception : assertThrown;
auto x = -1.checked!Throw;
assertThrown(x / 0);
```


More information about the Digitalmars-d mailing list