全部产品
云市场

com_android_internal_os_RuntimeInit_UncaughtHandler_uncaughtException

更新时间:2018-04-19 20:40:10

问题分析:

此类异常发生在应用创建了线程,如果线程因为异常未捕获而退出,在RuntimeInit中会捕获此类异常并杀死进程。具体异常的原因需要看日志附近其他log确定触发异常信息。

解决方案:

因为应用开启的线程抛出了异常未被捕获而导致抛出uncaughtException,建议在线程内捕获异常并做处理,如果无法在线程内捕获异常,设置线程默认的UncaughtException Handler来处理。

代码示例:

  1. public static void main(String[] args) {
  2. UncaughtExceptionHandler eh=new UncaughtExceptionHandler() {
  3. @Override
  4. public void uncaughtException(Thread t, Throwable e) {
  5. System.out.println("uncaught Caught " + e);
  6. }
  7. };
  8. Thread thread=new Thread(){
  9. @Override
  10. public void run(){
  11. try {
  12. // do something and happen a exception
  13. throw new IOException("e");
  14. } catch (IOException e) {
  15. System.out.println("catching IOException:");
  16. e.printStackTrace();
  17. }
  18. // code may cause some other exception, we could catch these, or use setDefaultUncaughtExceptionHandler
  19. System.out.println(1/0);
  20. }
  21. };
  22. thread.setDefaultUncaughtExceptionHandler(eh);
  23. thread.start();
  24. }

参考文献:

1.http://androidxref.com/4.3_r2.1/xref/frameworks/base/core/java/com/android/internal/os/RuntimeInit.java

2.http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.UncaughtExceptionHandler.html

3.http://stackoverflow.com/questions/6546193/how-to-catch-an-exception-from-a-thread

4.http://www.cnblogs.com/freeliver54/archive/2011/10/17/2215423.html