Division by zero - why no exception?
bearophile
bearophileHUGS at lycos.com
Sun Jan 10 02:45:45 PST 2010
> (but be careful, this works only in the main(), probably to keep tidy the semantics of pure functions):
That was not precise, the FPC struct is restored at the end of the main() scope, so if you call a function inside main it will keep the same FPC struct, and this raises the division by zero error:
import std.stdio: writeln;
import std.math: FloatingPointControl;
void foo() {
double a = 0.0;
double f = 1 / a;
writeln(f);
}
void main() {
FloatingPointControl fpc;
fpc.enableExceptions(FloatingPointControl.severeExceptions);
foo();
}
Also, you can't catch it:
void foo() {
double f = 0;
try {
double a = 0.0;
f = 1 / a; // object.Error: Float Divide by Zero
} catch (Error e) {
}
writeln(f);
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list