digitalmars.D - Catch div 0
- mogu (4/4) Jul 20 2019 Should d make div 0 a runtime exception? Or as an optional
- Johannes Loher (11/15) Jul 20 2019 I suggest you take a look at std.experimental.checkedint
- mogu (7/22) Jul 20 2019 Thanks. But if I use checked int, I must let all my libs support
- Johannes Loher (14/40) Jul 21 2019 I do not think the situation is as bad as you describe it. In
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.
Jul 20 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); ```
Jul 20 2019
On Saturday, 20 July 2019 at 21:11:34 UTC, Johannes Loher wrote:Am 20.07.19 um 19:31 schrieb mogu: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.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); ```
Jul 20 2019
On Sunday, 21 July 2019 at 01:59:53 UTC, mogu wrote:On Saturday, 20 July 2019 at 21:11:34 UTC, Johannes Loher wrote:I do not think the situation is as bad as you describe it. In particular the interaction of checked int with regular ints works reasonably well. But yes, you are right, code written by others can still result in the whole program being stopped by SIGFPE. For better or worse, checked int is the best solution you have. Throwing on division by 0 for regular ints will never get into D because that would mean that regular integer division (and modulo operation) is not nothrow anymore, which would prevent almost any code from being nothrow, rendering nothrow totally useless. Also it would make division slower because it would need to be checked in the language while currently the SIGFPE is basically raised by the processor.Am 20.07.19 um 19:31 schrieb mogu: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.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); ```
Jul 21 2019