0

Is there any way to catch all unhandled exceptions, even exceptions raise in distinct TThread ? The idea is to write these exceptions into a log file.

NOTE: Solution must work under iOs/Android ...

blong
  • 2,145
  • 13
  • 23
zeus
  • 12,173
  • 9
  • 63
  • 184
  • 4
    This is where is might be easier to just use MadExcept..but you should be able to do it by googling Application.OnException.. – John Easley Oct 14 '16 at 01:26
  • 5
    `Application.OnException` is not triggered for exceptions raised in a `TThread` or other worker thread, unless the thread catches the exception and passes it to `Application.HandleException()`. The RTL does that automatically in `Application.Run()` and VCL/FMX window procedures. To log exceptions globally, you need a logging library that hooks the RTL exception framework directly so it can handle uncaught exceptions on a per-thread basis. – Remy Lebeau Oct 14 '16 at 03:22
  • 2
    Use madExcept. Job done in 10 minutes. – David Heffernan Oct 14 '16 at 05:50
  • 1
    thanks, unfortunatly, MadExcept not work under iso/android as far as i know :( – zeus Oct 14 '16 at 08:36
  • That's a change to the question. – David Heffernan Oct 14 '16 at 23:17
  • The question did not originally say a Windows-only solution was sought and did not mention VCL, which would have implied that was required. madExcept is a Windows-only solution. As a reader, seeing clarification of the lack of target support of those comments comes across as quite reasonable. – blong Oct 17 '16 at 08:24

1 Answers1

7

As per the comments, rustling up some code is going to get you nowhere fast, so the smart money heads towards getting a product to the job for you.

In the interests of fairness I won't point you at my favourite but mention the popular products I have encountered:

And then there are Open Source options:

You can also hook exceptions using RtlUnwindProc as demonstrated for Win32 in this SO answer.

However, the primary drawback with all these options, as far as I know, is they only target Windows platforms. If you are targeting mobile (Android, iOS) or Macintosh then you'll unfortunately have to roll up your sleeves and ensure you put handlers into every thread you create and get familiar with Application.OnException, TThread.FatalException, et al.

** I haven't personally used these product so don't know if they pick up exceptions in all threads, but currently assume they do

Community
  • 1
  • 1
blong
  • 2,145
  • 13
  • 23
  • P.S. in case you resort to catching exceptions on mobile manually, to get a native call stack on Android you might look at [BackTrace](https://bitbucket.org/shadow_cs/delphi-arm-backtrace) by [Jan Rames](https://bitbucket.org/shadow_cs). – blong Oct 14 '16 at 08:32