Java – thread exit and uncapped exception: no stack trace
My application is causing a forced shutdown somewhere instead of using the usual (and very informative) stack trace in my logcat to get fat exception. I only receive the following four lines:
06-27 07:08:54.546: D/dalvikvm(14351): GC_FOR_MALLOC freed 9923 objects / 657416 bytes in 21ms 06-27 07:08:54.769: W/dalvikvm(14351): threadid=20: thread exiting with uncaught exception (group=0x4001d7f0) 06-27 07:08:54.796: W/dalvikvm(14351): threadid=21: thread exiting with uncaught exception (group=0x4001d7f0) 06-27 07:08:54.796: I/Process(14351): Sending signal. PID: 14351 SIG: 9
This is in debug mode and no filter is applied to logcat!
What might cause this behavior? Is there any way to tell what caused this exception?
Update: Thanks @ assylias. Below, I have been able to achieve:
final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread paramThread,Throwable paramThrowable) { Log.getStackTraceString(paramThrowable); subclass.uncaughtException(paramThread,paramThrowable); } });
Which generate these add lines:
06-27 08:24:47.105: D/dalvikvm(15475): GC_FOR_MALLOC freed 13865 objects / 1435952 bytes in 45ms 06-27 08:24:47.136: I/dalvikvm(15475): threadid=15: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI 06-27 08:24:47.136: I/dalvikvm(15475): method requires 28+20+20=68 bytes,fp is 0x45209338 (56 left) 06-27 08:24:47.140: I/dalvikvm(15475): expanding stack end (0x45209300 to 0x45209000) 06-27 08:24:47.140: I/dalvikvm(15475): Shrank stack (to 0x45209300,curFrame is 0x4520937c) 06-27 08:24:47.159: I/dalvikvm(15475): threadid=16: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI 06-27 08:24:47.159: I/dalvikvm(15475): method requires 28+20+20=68 bytes,fp is 0x4520c338 (56 left) 06-27 08:24:47.167: I/dalvikvm(15475): expanding stack end (0x4520c300 to 0x4520c000) 06-27 08:24:47.167: I/dalvikvm(15475): Shrank stack (to 0x4520c300,curFrame is 0x4520c37c) 06-27 08:24:47.175: I/dalvikvm(15475): threadid=17: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI 06-27 08:24:47.175: I/dalvikvm(15475): method requires 28+20+20=68 bytes,fp is 0x4520f338 (56 left) 06-27 08:24:47.175: I/dalvikvm(15475): expanding stack end (0x4520f300 to 0x4520f000) 06-27 08:24:47.175: I/dalvikvm(15475): Shrank stack (to 0x4520f300,curFrame is 0x4520f37c)
This is certainly more useful information, but now I am trying to solve the following problems:
>The application is not forced to close now, although subclass. Is called uncaughtException(). Why? > What does all this stack overflow mean? What can I do to tax so much on my bad Android test device? > How do I know which part of my code causes this?
Update: log getStackTraceString(paramThrowable); Nothing was actually printed The extra print I received was from the bogus subclass uncaughtException(paramThread,paramThrowable); The correct way to record a full stack trace is to use log e(TAG,“uncaughtException”,throwable).
Now the only question is how to re throw the exception? Just do a throw paramthrowable?
Answer my last question: eclipse won't let me throw no try / catch around, which leads me to understand that what I want is not a re throw, but a killprocess () The problem is solved
Solution
You can set a default uncapped exception handler at the beginning of the application and record some of its data (the following example uses a Java logger, but it is easy to transpose to Android):
private static void setDefaultUncaughtExceptionHandler() { try { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t,Throwable e) { logger.error("Uncaught Exception detected in thread {}",t,e); } }); } catch (SecurityException e) { logger.error("Could not set the Default Uncaught Exception Handler",e); } }