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:
- subclass
luper
and overrideloop ()
contains the code that callsRuns ()
Post Runnabels methodLooper
has not worked since last. - Override the subclass
handralread
and the run method which callsLooper.loop ()
. In the case of a runtime exception, call again and further tosuper.run ()
that was not working because "only one loop can be made per thread". - 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
Post a Comment