0

Inside my android service i would like to have a procedure to catch all unhandled exception. In app we do like this :

Application.OnException := MyExceptionCatcher;

However using Application.OnException inside a service require to include fmx.forms, and this will drastically increase the size of the .so (10x more bigger!)

how can i do ?

Also it's a very big mister for me, because doing

Application.OnException := MyExceptionCatcher

work perfectly, error is catch in MyExceptionCatcher (but we need to include fmx.forms that is not acceptable). But doing

System.Classes.ApplicationHandleException := MyExceptionCatcher

not work at all :(

zeus
  • 12,173
  • 9
  • 63
  • 184
  • Using `Fmx.Forms` and its `Application.OnException` isn't going to help because the Android service simply does not execute code inside a context that will fire `Application.OnException`. There's no place for `Fmx.Forms` in a service project, use that unit in GUI projects. – David Heffernan Jul 11 '17 at 08:47
  • aah ok, but how to track unhandled exception inside an android service ? – zeus Jul 11 '17 at 09:01
  • I don't know much about Android services (nothing in fact), but I'd expect it to be event driven. Which events of your `TAndroidService` object do you handle? – David Heffernan Jul 11 '17 at 09:04
  • I just do a test, and i confirm you that doing Application.OnException := myExceptionHandler; work under android service! it's catch the exception ! but size of the so is now much much more bigger – zeus Jul 11 '17 at 09:08
  • OK, clearly I don't understand this at all. I do wonder why you don't just catch exceptions in your top level event handler. – David Heffernan Jul 11 '17 at 09:10
  • and where is the top level event handler ? for exemple now i have a exception in the java listener onLocationChanged. without Application.OnException i have no way to catch this exception. i need to make an exception handled arount the service looper i think – zeus Jul 11 '17 at 09:12
  • Surely you have some code that implements the service? I'd expect this to be called from an event attached to `TAndroidService`. Of course, Embarcadero don't have documentation for `TAndroidService`. I mean, after all, why would it be useful to anybody to be able to read documentation? – David Heffernan Jul 11 '17 at 09:18
  • yea :( no right now i can't catch the exception without Application.OnException :( exception is raise inside a hidden java event (that is fired when the user move the phone) and not inside a dephi pascal code :( so i don't have any entry point inside my service pascal code – zeus Jul 11 '17 at 09:29
  • My advice is to write your service in Java, especially if you care about its size. Not only will the resulting executable be smaller, it will work reliably across a broader selection of devices. And you'll have decent documentation, loads of examples, and so on. – David Heffernan Jul 11 '17 at 09:30
  • I fully agree about writing it in java! but Unfortunatly this service contain a quite big code that i use, and it's the same code i use also for ios so i don't want to rewrite it in java :( but clearly it's look much much more efficient to write service in java – zeus Jul 11 '17 at 12:30
  • but what i absolutely not understand is why the size of the so is 3x more bigger when i include use fmx.forms when it's seam that the unit is in anycase use internally by the rtl ... this is a big mistery – zeus Jul 11 '17 at 12:35
  • No, it's not used by the rtl – David Heffernan Jul 11 '17 at 12:52
  • so how you explain that just doing Application.OnException := MyExceptionCatcher; work in this way ? so the rtl must in anycase access somewhere the Application.OnException that is declared inside the fmx.forms – zeus Jul 11 '17 at 13:22
  • oooh i see how they did : constructor TApplication.Create(AOwner: TComponent); begin inherited; if not Assigned(System.Classes.ApplicationHandleException) then System.Classes.ApplicationHandleException := HandleException; – zeus Jul 11 '17 at 13:24
  • OK, looks like you have an answer then. Assign to `System.Classes.ApplicationHandleException`. – David Heffernan Jul 11 '17 at 13:28
  • i try it now, wait 2 min (7 with the compile time under android) and i will say you :) – zeus Jul 11 '17 at 13:28
  • I don't think that will catch exceptions that occur in Java code. You might be better off looking at this: https://stackoverflow.com/questions/19897628/need-to-handle-uncaught-exception-and-send-log-file. I had planned on visiting this myself before now, however other things have taken precedence – Dave Nottage Jul 11 '17 at 13:56
  • @DavidHeffernan : no System.Classes.ApplicationHandleException := myexeptionhandler do not work :( – zeus Jul 11 '17 at 14:18
  • 1
    @DaveNottage i don't know, with Application.OnException := myExceptionHandler it's work it's just what i know – zeus Jul 11 '17 at 14:31
  • I updated the question, because i really can't understand why System.Classes.ApplicationHandleException := MyExceptionCatcher not work and Application.OnException := MyExceptionCatcher work. – zeus Jul 11 '17 at 15:04
  • finally i gave up :( too much buggy the delphi android services... use quite big amout of disk space (around 20 mo for the so that is close to be empty), crash often (i don't know why, but crash), compile is more slow now and don't work under android (because https://stackoverflow.com/questions/45040463/why-when-we-add-an-android-service-to-an-app-delphi-automatically-include-in-th) ... so i do the service in Java ... – zeus Jul 11 '17 at 20:57
  • Application.OnException is called by TApplication.HandleException, which is assigned to ApplicationHandleException if a TApplication is created. If your assignment does not work it means that it is never called, for instance if TApplication is never created, CheckSynchronize will not be called from the Idle method. – Dave Nottage Jul 11 '17 at 21:10

0 Answers0