2

I am using Delphi 10.3 to develop an application for Mac OSX Big Sur 64-bit.

I have run into a problem trying to catch exceptions, and I cannot find the solution.

When an exception occurs, the execution cycle does not go into the try/except. It seems that the RTL gives an error when raising the Exception, triggering a new 'Access Violation' exception that goes into a loop.

Below, you can find the test code that produces the problem:

Unit8;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm8 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function Str2Float(str : String) : Extended;

var
  Form8: TForm8;

implementation

{$R *.fmx}

procedure TForm8.Button1Click(Sender: TObject);
var
  f : extended;
begin
  f:=str2float('10,7');
  ShowMessage ('Resultat : '+floattostr(f));
end;

function Str2Float(str : String) : Extended;
var
  dec : Char;
begin
  result := 0;
  if length(str) > 0 then begin
    dec := FormatSettings.DecimalSeparator;
    FormatSettings.DecimalSeparator := '.';
    try
      result := strtofloat(str);
    except
      on e : Exception do begin
        FormatSettings.DecimalSeparator := ',';
        try
          result := strtofloat(str);
        except
          on e : Exception do begin
            result := 0;
          end;
        end;
      end;
    end;
    FormatSettings.DecimalSeparator := dec;
  end;
end;

end.

Exception that is thrown:

Image of Dialog of Exception

And the state of the Call Stack when the exception is thrown:

Image of Stack

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
IndexCons
  • 31
  • 4
  • 1
    Not a direct solution, but use TryStrToFloat instead of StrToFloat. This will make your code ligther and cleaner. And use a try/finally to save/protect/restore the decimal separator. – fpiette Feb 25 '21 at 14:08
  • 4
    Also, stop using the global `FormatSettings` - that's a legacy variable, it should never go into new code. Create a local `TFormatSettings` and use that instead. Take every opportunity to shed globals. – J... Feb 25 '21 at 14:20
  • As for the rest of the question `it seems that RTL gives an error` - which error? We can't see your screen. Can you provide the stack trace and the full error text? – J... Feb 25 '21 at 14:21
  • Sorry, I know the code is not perfect, it's just an example to show the problem I'm having, I can't catch any kind of exception using Delphi's Try-Except. I create a new answer to show the exception that is thrown and also to show the state of the stack when the exception is thrown. Thanks. – IndexCons Feb 25 '21 at 15:48
  • Use `function StrToFloat(const S: string; const AFormatSettings: TFormatSettings): Extended;` or its `Try...` derivate to supply a local format for the conversion. Tampering with the global `FormatSetting` is not good at all, as @J... mentions. As for adding images for error messages and call stacks, please add them as text instead. – LU RD Feb 25 '21 at 16:24
  • 3
    @IndexCons Now that you've at least linked images of your exception, it seems that you are confusing the debugger's *first chance exception* with the actual program error handling flow. Please see the linked duplicates and, in particular, Rob's answer to the first. – J... Feb 25 '21 at 16:32
  • This question was incorrectly marked as a duplicate. Unfortunately the real answer is that there is no support for structured exception handling on MacOS from Delphi, if the exception originates outside of the RTL/Delphi classes. This s flippantly documented here: https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Differences_between_Windows_and_macOS It would have been nice to see an actual discussion about how to guarantee exception cleanup code execution instead of harping on the cause of the exception. – Brandon Staggs Feb 01 '23 at 16:36
  • 2
    I removed wrong duplicates from this question, and added appropriate one. If you still think duplicate does not cover the behavior you are experiencing, please edit the question and explain the problem with the solutions posted there. – Dalija Prasnikar Feb 01 '23 at 19:20

0 Answers0