Catch div 0

mogu mogucpp at 163.com
Sun Jul 21 01:59:53 UTC 2019


On Saturday, 20 July 2019 at 21:11:34 UTC, Johannes Loher wrote:
> 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);
> ```

Thanks. But if I use checked int, I must let all my libs support 
checked int. Otherwise I must take care of all the parts using 
ints from checked in order to safely interacting with other libs.
As a designer, I wish other logic programmers will not break down 
the whole program suddenly. But now I have to use multi-process 
instead of only a single one to handle it.


More information about the Digitalmars-d mailing list