Recent

Author Topic: [SOLVED] LAMW - jExpression ends application on errors  (Read 1973 times)

guaracy

  • New Member
  • *
  • Posts: 13
[SOLVED] LAMW - jExpression ends application on errors
« on: October 04, 2021, 03:31:11 pm »
Any way to catch java exceptions?

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  2. var
  3.   r: Double;
  4. begin
  5.   jExpression1.SetFormula('3*(2+2)',[]);  // OK
  6.   jExpression1.SetFormula('2/0',[]);  // crash : Division by zero
  7.   jExpression1.SetFormula('2/4+',[]); // crash : Invalid number of operands available for '+'
  8.   try
  9.     r:=jExpression1.Evaluate();
  10.   except
  11.     ShowMessage('ERROR');
  12.   end;
  13.   ShowMessage(FloatToStr(r));
  14. end;  
  15.  

« Last Edit: October 12, 2021, 07:15:25 pm by guaracy »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1987
Re: LAMW - jExpression ends application on errors
« Reply #1 on: October 04, 2021, 07:16:37 pm »

Hi, guaracy!

I will try improve the jExpression component!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

jmpessoa

  • Hero Member
  • *****
  • Posts: 1987
Re: LAMW - jExpression ends application on errors
« Reply #2 on: October 06, 2021, 12:45:44 am »
Done!

1) Added "inner" java handling to ArithmeticException ("Division by zero")...

2) New methods
Code: Pascal  [Select][+][-]
  1.     function IsExpressionValid(_checkVariablesSet: boolean): boolean;
  2.  
  3.     function CanEvaluate(_checkVariableSet: boolean): boolean;
  4.     function GetValue(): double;    
  5.  

3) Updated demo "AppExpressionDemo1"

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

engkin

  • Hero Member
  • *****
  • Posts: 2977
Re: LAMW - jExpression ends application on errors
« Reply #3 on: October 06, 2021, 02:17:00 am »
In general, exceptions inside Java code can be tested with ExceptionOccurred, and should be cleared with ExceptionClear.
Without the latest update by jmpessoa you can write:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jButton1Click(Sender:TObject);
  2. {$push}{$WriteableConst+}
  3. const
  4.   eIdx:integer=0;{$pop}
  5. var
  6.   e:array[0..2] of String=('3*(2+2)','2/0','2/4+');
  7.   r:string;
  8. begin
  9.   jExpression1.SetFormula(e[eIdx],[]);
  10.   r:=jExpression1.Evaluate.ToString;
  11.   if gApp.Jni.jEnv^.ExceptionOccurred(gApp.Jni.jEnv)<>nil then
  12.   begin
  13.     gApp.Jni.jEnv^.ExceptionClear(gApp.Jni.jEnv);//<--- if you don't clear it, your app will be killed
  14.     r:='ERROR';
  15.   end;
  16.  
  17.   ShowMessage(e[eIdx]+' = '+r);
  18.   inc(eIdx); if eIdx>High(e) then eIdx:=0;
  19. end

guaracy

  • New Member
  • *
  • Posts: 13
Re: LAMW - jExpression ends application on errors
« Reply #4 on: October 08, 2021, 08:02:07 pm »
Thanks jmpessoa.

Errors like unbalanced parentheses must be catch inside SetFormula function in Java. Otherwise the application is aborted.

Sorry for the code (my first Java lines) but worked here.

Code: Java  [Select][+][-]
  1.     public void SetFormula(String _expression, String[] _variables) {
  2.         try {
  3.             mExprBuilder = new ExpressionBuilder(_expression);
  4.             mExprBuilder.variables( _variables);
  5.             mExpr = mExprBuilder.build();
  6.         } catch (Exception e) {
  7.             mExpr = null;
  8.             Toast toast = Toast.makeText(controls.activity, "Exception on Builder!"/*e.getMessage()*/, Toast.LENGTH_LONG);
  9.             if (toast != null) {
  10.                 //toast.setGravity(Gravity.BOTTOM, 0, 0);
  11.                 toast.show();
  12.             }
  13.             Log.e("LAMW", "Exception on Builder!", e);
  14.         }
  15.     }
  16.  

jmpessoa

  • Hero Member
  • *****
  • Posts: 1987
Re: LAMW - jExpression ends application on errors
« Reply #5 on: October 08, 2021, 08:52:12 pm »
Quote
Sorry for the code (my first Java lines) but worked here.

Good code!

I will improve based on your code!

Thank you!

P.S.

Done!
« Last Edit: October 09, 2021, 04:45:16 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018