Recent

Author Topic: fpExpressionParser Error Trapping  (Read 1961 times)

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
fpExpressionParser Error Trapping
« on: October 20, 2018, 05:10:57 pm »
Hello --
I am using TfpExpressionParser in a small demo program and cannot figure out how to trap exceptions the parser generates.  Below is the code I've tried.  I think I am misunderstanding how 'try .. except' statements work.

The parser is initialized by:

    Parser := TFPExpressionParser.Create(self);
    Parser.BuiltIns := [bcMath];
   
in the FormActivate procedure and it evaluates with:

procedure TMain.btnCalcClick(Sender: TObject);
  var
    ans: Boolean;
  begin
    try
      Parser.Expression:=edtInput.Text;
      ans:=Parser.Evaluate.ResBoolean;
    except
      On E: Exception do begin
        ShowMessage('Error: '+E.Classname +#10#13+ E.Message);
        //Execute Error correction steps
      end;
    end;
    if ans then edtResult.Text:='True'
    else edtResult.Text:='False';
  end;

If edtInput.Text is "2 + * 3 = 5", the parser throws the error "EExprParser, Unknown token at pos 5".  The code never enters the "except" section.  How do I get the except section to run when an exception occurs?

Lazarus 1.8.4,  FPC 3.04

Thanks
Bert
Windows 10, Lazarus 2.2.2, FPC 3.2.2

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: fpExpressionParser Error Trapping
« Reply #1 on: October 20, 2018, 05:19:00 pm »
The IDE by default traps all exceptions. Try outside the ide

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Re: fpExpressionParser Error Trapping
« Reply #2 on: October 20, 2018, 05:25:49 pm »
Thanks for quick reply.  Same result when running the program outside the IDE.
Windows 10, Lazarus 2.2.2, FPC 3.2.2

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: fpExpressionParser Error Trapping
« Reply #3 on: October 20, 2018, 05:52:51 pm »

I don't know fpexprpars that well (I usually use the expression parser in unit symbolic). I quickly tested, and for me it works:

Code: Pascal  [Select][+][-]
  1. uses fpexprpars,sysutils,classes;
  2.  
  3. var
  4.     ans: Boolean;
  5.     Parser:tfpexpressionparser;
  6.   begin
  7.     Parser:=tfpexpressionparser.create(NIL);
  8.     Parser.BuiltIns := [bcMath];
  9.     try
  10.       Parser.Expression:='2 + * 3 = 5';
  11.       ans:=Parser.Evaluate.ResBoolean;
  12.     except
  13.       On E: Exception do begin
  14.       writeln('Error: '+E.Classname +#10#13+ E.Message);
  15.         //Execute Error correction steps
  16.       end;
  17.     end;
  18.     writeln(ans);
  19.   end.

outputs:

Code: Pascal  [Select][+][-]
  1.  
  2. Error: EExprParser
  3. Unknown token at pos 6 : *
  4. FALSE
  5.  

And no signs of traceback or other uncaught exceptions.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: fpExpressionParser Error Trapping
« Reply #4 on: October 20, 2018, 06:44:47 pm »
If edtInput.Text is "2 + * 3 = 5", the parser throws the error "EExprParser, Unknown token at pos 5".  The code never enters the "except" section.  How do I get the except section to run when an exception occurs?
Maybe your are not patient enough. When you run an program in the IDE (in the debugger) and an exception occurs, the exception is always handled by the IDE which shows the exception dialog. Your own dialog is shown only when you press "Continue" (or so). When you run the program outside the IDE (or select "Run" > "Run without Debugging") only your own exception handling is executed.

You can avoid the exception handling by the IDE by adding the exception class to the list in "Tools" > "Debugger" > "Language exceptions".  The exceptions of the fpexpressionparser are EExprScanner and EExprParser. Click on "Add" and enter these names (one by one, of course), and your program should display your own exception message. Note that the entries in this dialog, although in a Lazarus-specific form, are valid only for the current project.

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Re: fpExpressionParser Error Trapping
« Reply #5 on: October 20, 2018, 06:54:35 pm »
marcov --

Thanks for your help.  I indeed got your version to work successfully!  And upon deeper inspection, I now discover that my version was/is also working!  Sorry to bother everyone.  Case closed.
Windows 10, Lazarus 2.2.2, FPC 3.2.2

 

TinyPortal © 2005-2018