www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Catch div 0

reply mogu <mogucpp 163.com> writes:
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
parent reply Johannes Loher <johannes.loher fg4f.de> writes:
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
parent reply mogu <mogucpp 163.com> writes:
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.
Jul 20 2019
parent Johannes Loher <johannes.loher fg4f.de> writes:
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:
 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.
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.
Jul 21 2019