Recent

Author Topic: How to handling uncaught exception easier in LAMW like in LCL?  (Read 2599 times)

Dio Affriza

  • Full Member
  • ***
  • Posts: 100
Currently, I've modified Laz_And_Controls jAsyncTask to become like bellow, because I'm too lazy to make try..except on every task. Not just that, I add "keepInBackground := False", because adding that into every task doesn't look makes sense to me.
Code: Pascal  [Select][+][-]
  1.  procedure jAsyncTask.GenEvent_OnAsyncEventDoInBackground(Obj: TObject; progress: integer; out keepInBackground: boolean);
  2. begin
  3.   keepInBackground:= True;
  4.   fExceptStr := '';
  5.   try
  6.     if Assigned(FOnDoInBackground) then
  7.       FOnDoInBackground(Obj,progress,keepInBackground);
  8.     keepInBackground := False;
  9.   except
  10.     on e: exception do
  11.     begin
  12.       keepInBackground := False;
  13.       fExceptStr := e.Message;
  14.     end;
  15.   end;
  16. end;
  17.  
  18. procedure jAsyncTask.GenEvent_OnAsyncEventPostExecute(Obj: TObject; progress: Integer);
  19. begin
  20.   if fExceptStr <> '' then
  21.     gApp.ShowMessage('ASyncTask Exception Error', fExceptStr, 'OK');
  22.   if Assigned(FOnPostExecute) then FOnPostExecute(Obj,progress);
  23. end;
  24.  

But still, the kinda unsatisfying result happens, it doesn't handle nil variable. It's okay for now because it handles most exceptions except the Access Violation.

I was trying to look to any LAMW sources because there's no loop in the jApp (everything handled by events) so jApp.OnException is impossible to use.

I've tried in this thread to modify the App.java template, but I have no idea because I don't speak Java. Maybe someone can tell me how to use this?

Does anyone have an idea?

Thank you.  :D
« Last Edit: August 29, 2019, 09:30:52 am by Dio Affriza »

Thaddy

  • Hero Member
  • *****
  • Posts: 10991
Re: How to handling uncaught exception easier in LAMW like in LCL?
« Reply #1 on: August 29, 2019, 09:48:51 am »
You can use this: https://www.freepascal.org/docs-html/rtl/system/errorproc.html
Be careful if you use sysutils, this needs to be declared *after* sysutils and you need to call the sysutils version for everything you do not handle yourself.(Need to save the sysutils version's address in that case.)

Note that on Android, you need the Java exception handling too, to Freepascal it is an external exception. That is easy to do, but not as easy as try/except.
« Last Edit: August 29, 2019, 09:50:38 am by Thaddy »
The average programmer productivity is 4-5 hours per day. Peak performance 72 hours for short bursts. MTBF is 1 second or less.

Dio Affriza

  • Full Member
  • ***
  • Posts: 100
Re: How to handling uncaught exception easier in LAMW like in LCL?
« Reply #2 on: August 29, 2019, 12:11:55 pm »
I do not really understand how to use that.
Quote
this needs to be declared *after* sysutils
What does that mean, is that can be used in library? Can I get the sample usage?

Like this one maybe?
Code: Pascal  [Select][+][-]
  1. library controls;  //[by LAMW: Lazarus Android Module Wizard: 22/08/2019 13:12:35]
  2.  
  3. {$mode delphi}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes, SysUtils, And_jni, And_jni_Bridge, AndroidWidget, Laz_And_Controls,
  10.   Laz_And_Controls_Events, control_main;
  11.  
  12. {%region /fold 'LAMW generated code'}
  13.  
  14. procedure OnError(ErrNo: LongInt; Address: CodePointer; Frame: Pointer);
  15. begin
  16.   gApp.ShowMessage('An Error Occured!', 'Exception error at address: '+
  17.     sysBackTraceStr(Address), 'OK');
  18. end;
  19.  
  20. begin
  21.   // I'm not quite sure about this
  22.   ErrorProc := OnError;
  23.   gApp := jApp.Create(nil);
  24.   gApp.Title := 'LAMW JNI Android Bridges Library';
  25.   gjAppName := 'id.estoh.tumbas.stockwriter';
  26.   gjClassName := 'id/estoh/tumbas/stockwriter/Controls';
  27.   gApp.AppName := gjAppName;
  28.   gApp.ClassName := gjClassName;
  29.   gApp.Initialize;
  30.   gApp.CreateForm(TControlMain, ControlMain);
  31. end.            
  32.  

Btw, I'm just lost my mind maybe. So, I transformed all LAMW events like this  :-[
Code: Pascal  [Select][+][-]
  1. Procedure Java_Event_pOnClick(env: PJNIEnv; this: jobject; Obj: TObject; Value: integer);
  2. begin
  3.  
  4.   //----update global "gApp": to whom it may concern------
  5.   gApp.Jni.jEnv:= env;
  6.   gApp.Jni.jThis:= this;
  7.   //------------------------------------------------------
  8.   // here we go
  9.   try
  10.     if not (Assigned(Obj)) then Exit;
  11.     if Obj is jForm then
  12.     begin
  13.       jForm(Obj).UpdateJNI(gApp);
  14.       jForm(Obj).GenEvent_OnClick(Obj);
  15.       Exit;
  16.     end;
  17.     // and etc.
  18.  
  19.   except
  20.     on e: exception do
  21.     begin
  22.       gApp.ShowMessage('Exception Error!', e.Message, 'OK');
  23.     end;
  24.   end;
  25. end;
  26.  
« Last Edit: August 29, 2019, 12:17:48 pm by Dio Affriza »

commanderz

  • Newbie
  • Posts: 4
Re: How to handling uncaught exception easier in LAMW like in LCL?
« Reply #3 on: October 09, 2021, 04:55:09 pm »
Very useful information, helped me get rid of the errors that the "raise" method generated in the Indy library.
Thanks to author!

jmpessoa

  • Hero Member
  • *****
  • Posts: 1988
Re: How to handling uncaught exception easier in LAMW like in LCL?
« Reply #4 on: October 09, 2021, 07:15:47 pm »
Quote
So, I transformed all LAMW events like this..

Good!

I will try apply your solution to LAMW code!

Thank you!

SIDE NOTE:

Quote
Ref.
https://forum.lazarus.freepascal.org/index.php/topic,56590.0.html


By engkin

In general, exceptions inside Java code can be tested with ExceptionOccurred,
and should be cleared with ExceptionClear.


procedure TAndroidModule1.jButton1Click(Sender:TObject);
{$push}{$WriteableConst+}
const
  eIdx:integer=0;{$pop}
var
  e:array[0..2] of String=('3*(2+2)','2/0','2/4+');
  r:string;
begin
  jExpression1.SetFormula(e[eIdx],[]);
  r:=jExpression1.Evaluate.ToString;
  if gApp.Jni.jEnv^.ExceptionOccurred(gApp.Jni.jEnv)<>nil then
  begin
    gApp.Jni.jEnv^.ExceptionClear(gApp.Jni.jEnv);//<--- if you don't clear it, your app will be killed
    r:='ERROR';
  end;
 
  ShowMessage(e[eIdx]+' = '+r);
  inc(eIdx); if eIdx>High(e) then eIdx:=0;
end;
« Last Edit: October 09, 2021, 07:20:17 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018