android - Avoid crashing the Handler thread in case of Runtime Exception? -


I have a handler (thread) on which I post Runnables if a runnab accidentally throws a runtime exception The entire app process crashes. Looks sensible as a default, but I want to catch exceptions like this sort, log an error, and continue running I have difficulty working on this, though. I thought about the following options:

  1. subclass luper and override loop () contains the code that calls Runs () Post Runnabels method Looper has not worked since last.
  2. Override the subclass handralread and the run method which calls Looper.loop () . In the case of a runtime exception, call again and further to super.run () that was not working because "only one loop can be made per thread".
  3. In each runable category, include an attempt-to-run for runtime exceptions, this will work, but rather annoying because I have many different runner classes. Besides, handler will not protect the thread If forgetful enough to forget, please include try-catch.

In a whole world, Looper is a member method called registerExceptionHandler (ExceptionHandler) and exception handler In the case of exceptions will be called.

Has anybody got better suggestions?

Try this custom handler:

  Class H handler spreads {Public H (Looper Lupper) {Super (Looper); } @ Override public blank send message (message message) {// try to catch an exception {super.dispatchMessage (msg); } Hold (exception e) {log d. (Tag, "Send Message" + E. Message ()); }}}  

Test code:

  handralread HT = new handralread ("hthread"); Ht.start (); Handler H = New H (HT Gal) Lupper (); Runnabal R = New Runnabal () {@ Override Public Wide Run (New Runtime Expansion ("Testing Exception");}}; h.post (r); h.post (r);  

Comments