EOverflow Exception and +INF in COM DLL

346 Views Asked by At

In Delphi Seattle, I have a calculation function of the following construct:

try
  result := ..... // calculation
except
  // Handle (overflow) exception
end;

Calling this function from an EXE, works as expected and the overflow exception is trapped and handled. However, calling the function from a COM DLL, the exception is not trapped. By debugging, I figured out that result becomes +INF and the EOverflow exception is not raised (which has a knock-on effect further in my code).

After some experimentation I figured out that changing the function to the following does work from the COM DLL:

try
  result := ..... // calculation
  if result = Infinity then
    raise EOverflow.Create('Forced overflow');
except
  // Handle (overflow) exception
end;

I suppose I could wrap the 'if result = ... ' code in an {$IFDEF MYFLAG} block so that it only compiles for the COM DLL (which is fine as a quick-fix), but I am wondering if there's a more elegant solution.

Doing some research on the web, I have come across the suggestion to use SetExceptionMask or the $FINITEFLOAT compiler directive. SetExceptionMask seems rather tricky (side effects?) and $FINITEFLOAT seems to have no effect, which I more or less expected since ON is supposed to be the default...

Moreover, the suggestions found are all for older (even .NET) versions of Delphi, which makes me a bit weary to apply it.

Any thoughts or suggestions? Thanks in advance for considering!

0

There are 0 best solutions below