In recent Delphi you cannot rely on dividing by zero to raise an exception

This is mostly a note to self:

Having been stuck with Delphi 2007 for many years I didn’t notice that dividing floating point values by zero no longer raises an exception in recent Delphi versions. Apparently this changed around the time of Delphi 10.1, even though I found a blog post by Dalija Prasnikar saying this changed in Delphi 12.

Apparently this has always been the case in other programming languages.

So the following code will run just “fine”.

var
  x, y: double;
begin
  x := 0;
  y := x / 0; // no EInvalidOp "Invalid floating point operation"
  // y is now -NAN
  x := 5;
  y := x / 0; // no EZeroDivide "Floating point division by zero"
  // y is now +INF
end;

Also I wasn’t really aware that 0/0 used to raise EInvalidOp rather than EZeroDivide.

This removes another safety feature for which I had previously relied on the compiler and the runtime library.

I tried enabling these exceptions using

  Mask := GetExceptionMask;
  SetExceptionMask(Mask - [exZeroDivide, exInvalidOp]);

but that caused these exceptions popping up in other parts of the code all of a sudden, e.g. SysUtils.TryEncodeTime followed by an Access Violation in jclDebug, which is even worse.

The Math unit contains some other interesting functions of which I wasn’t aware:

procedure RaiseExceptions(const ExceptionFlags: TArithmeticExceptions);

which in turn calls

procedure ClearExceptions(RaisePending: Boolean = True; ExceptionFlags: TArithmeticExceptions = DefaultExceptionFlags);

They don’t seem be be called anywhere in the RTL source code to which I have access.