Delphi Berlin 10.1 Division with zero exception

I wonder if you don't get the division by zero exception. How do I get it back?

Berlin 10.1 a very new installation, a new project,

procedure TForm1.Button1Click(Sender: TObject); var a: Double; begin a := 5/0; // No exception ShowMessage(a.ToString); // -> 'INF' end; 
+5
source share
2 answers
 a := 5/0; 

The expression 5/0 , from a technical point of view, is an expression.

A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numbers; character strings; true constants; values โ€‹โ€‹of enumerated types; special constants True, False and nil; and expressions built exclusively from these elements with set operators, types, and constructors.

Thus, this expression is evaluated by the compiler, and not at runtime. Thus, its evaluation is determined by compilation time rules and cannot depend on runtime exclusion masks.

And these rules state that a positive value divided by zero is +INF , the special value of IEEE754. If you modify the expression to have at least one argument that is not a constant expression, then it will be evaluated at runtime, and the selection with zero exception will be increased.

+7
source

You can control which floating point exceptions are thrown using the SetExceptionMask function in the Math module: http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Math.SetExceptionMask

To disable all floating point exceptions, use:

 SetExceptionMask(exAllArithmeticExceptions); 

To enable all use of floating point exceptions:

 SetExceptionMask([]); 

Also note that in your code example, the compiler will determine the value at compile time (since the expression is constant), so it will not throw an exception regardless of the value passed to SetExceptionMask. To throw an exception, you will need a slightly more complex example, something like this:

 program test; uses Math; var xx,yy: double; begin SetExceptionMask([]); xx := 1; yy := 0; halt(round(xx/yy)); end. 
+4
source

Source: https://habr.com/ru/post/1273813/


All Articles