How to handling uncaught exception easier in LAMW like in LCL? lazarus / 43-Android ---------------------------------------------------------------------- AFFRIZA 亜風実 · 2019-08-29 03:37 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure jAsyncTask.GenEvent_OnAsyncEventDoInBackground(Obj: TObject; progress: integer; out keepInBackground: boolean);begin  keepInBackground:= True;  fExceptStr := '';  try    if Assigned(FOnDoInBackground) then    FOnDoInBackground(Obj,progress,keepInBackground);    keepInBackground := False;  except    on e: exception do    begin    keepInBackground := False;      fExceptStr := e.Message;    end;  end;end;  procedure jAsyncTask.GenEvent_OnAsyncEventPostExecute(Obj: TObject; progress: Integer);begin  if fExceptStr <> '' then    gApp.ShowMessage('ASyncTask Exception Error', fExceptStr, 'OK');  if Assigned(FOnPostExecute) then FOnPostExecute(Obj,progress);end;  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. ---------------------------------------------------------------------- Thaddy · 2019-08-29 07:48 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. ---------------------------------------------------------------------- AFFRIZA 亜風実 · 2019-08-29 10:11 I do not really understand how to use that. Quotethis needs to be declared *after* sysutilsWhat does that mean, is that can be used in library? Can I get the sample usage? Like this one maybe? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i library controls;  //[by LAMW: Lazarus Android Module Wizard: 22/08/2019 13:12:35]  {$mode delphi}  uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes, SysUtils, And_jni, And_jni_Bridge, AndroidWidget, Laz_And_Controls,  Laz_And_Controls_Events, control_main;  {%region /fold 'LAMW generated code'}  procedure OnError(ErrNo: LongInt; Address: CodePointer; Frame: Pointer);begin  gApp.ShowMessage('An Error Occured!', 'Exception error at address: '+    sysBackTraceStr(Address), 'OK');end; begin  // I'm not quite sure about this  ErrorProc := OnError;   gApp := jApp.Create(nil);  gApp.Title := 'LAMW JNI Android Bridges Library';  gjAppName := 'id.estoh.tumbas.stockwriter';  gjClassName := 'id/estoh/tumbas/stockwriter/Controls';  gApp.AppName := gjAppName;  gApp.ClassName := gjClassName;  gApp.Initialize;  gApp.CreateForm(TControlMain, ControlMain);end.           Btw, I'm just lost my mind maybe. So, I transformed all LAMW events like this  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure Java_Event_pOnClick(env: PJNIEnv; this: jobject; Obj: TObject; Value: integer);begin   //----update global "gApp": to whom it may concern------  gApp.Jni.jEnv:= env;  gApp.Jni.jThis:= this;  //------------------------------------------------------  // here we go  try    if not (Assigned(Obj)) then Exit;    if Obj is jForm then  begin      jForm(Obj).UpdateJNI(gApp);      jForm(Obj).GenEvent_OnClick(Obj);      Exit;    end;    // and etc.   except    on e: exception do    begin      gApp.ShowMessage('Exception Error!', e.Message, 'OK');    end;  end;end; ---------------------------------------------------------------------- commanderz · 2021-10-09 14:55 Very useful information, helped me get rid of the errors that the "raise" method generated in the Indy library. Thanks to author! ---------------------------------------------------------------------- jmpessoa · 2021-10-09 17:15 QuoteSo, I transformed all LAMW events like this.. Good! I will try apply your solution to LAMW code! Thank you! SIDE NOTE: QuoteRef. 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;