简体   繁体   English

德尔福柏林10.1分零缺失异常

[英]Delphi Berlin 10.1 Division by zero exception missing

I am surprised not the get division by zero exception. 我很惊讶没有得到除零异常。 How do I get it back? 我该如何取回它?

Berlin 10.1 very recent install, new project, 柏林10.1最近安装,新项目,

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

The expression 5/0 is, in technical language terms, a constant expression . 在技​​术语言术语中,表达式5/0常量表达式

A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. 常量表达式是编译器可以在不执行其发生的程序的情况下进行求值的表达式。 Constant expressions include numerals; 常量表达式包括数字; character strings; 字符串; true constants; 真常数; values of enumerated types; 枚举类型的值; the special constants True, False, and nil; 特殊常量True,False和nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors. 和表达式专门使用运算符,类型转换和集合构造函数构建的表达式。

As such this expression is evaluated by the compiler, and not at runtime. 因此,此表达式由编译器评估,而不是在运行时。 So its evaluation is determined by compile time rules and cannot be influenced by runtime floating point unit exception masks. 因此,它的评估由编译时规则决定,不受运行时浮点单元异常掩码的影响。

And those rules state that positive value divided by zero is equal to +INF , a special IEEE754 value. 这些规则规定正值除以零等于+INF ,一个特殊的IEEE754值。 If you change the expression to have at least one argument that is not a constant expression then it will be evaluated at runtime, and a divide by zero exception will be raised. 如果将表达式更改为至少有一个不是常量表达式的参数,那么它将在运行时进行计算,并且将引发除零异常。

You can control which floating point exceptions are triggered using the SetExceptionMask function in Math unit: http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Math.SetExceptionMask 您可以使用Math单元中的SetExceptionMask函数控制触发哪些浮点异常: http ://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Math.SetExceptionMask

To disable all floating point exceptions use: 要禁用所有浮点异常,请使用:

SetExceptionMask(exAllArithmeticExceptions);

To enable all floating point exception use: 要启用所有浮点异常,请使用:

SetExceptionMask([]);

Also note that in your example code the compiler will determine the value at compile time (since the expression is constant) so it will not trigger an exception regardless of the value passed to SetExceptionMask. 另请注意,在您的示例代码中,编译器将在编译时确定值(因为表达式是常量),因此无论传递给SetExceptionMask的值如何,它都不会触发异常。 To trigger exception you 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM