对于runtime exception引起的crash
eg. int i = 100/0; 引起的java.lang.ArithmeticException
在/data/data/com.qihoo.browser下创建app_crash目录, 并创建一个*.stacktrace文件,以json格式保存调用栈.
code 实现:
ChromeApplication.java
->
public class CrashHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
//通过e创建一个CrashMessage对象, 之后创建.stacktrace文件, 保存调用栈信息.
CrashUploadManager.save(new CrashMessage(t, e, sProcessType, CrashMessageExtra.get()));
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
public static void init() {
Thread.setDefaultUncaughtExceptionHandler(
new CrashHandler(Thread.getDefaultUncaughtExceptionHandler()));
}
}
log中搜tag: Process 来定位crash的位置:
Process I Sending signal. PID: 21253 SIG: 9
对于caught exception,模拟一个IOException
try {
FileInputStream fis = new FileInputStream(“/data/data/com.qihoo.browser/files/DecRawsoLib/libchrome_public2.so”);
fis.read();
} catch (IOException e) {
e.printStackTrace();
BLog.e("ahking", "FileInputStream read()", e);
}
调用栈的内容会追加到caught_crashes.txt的末尾.
/storage/emulated/legacy/360browser/log/caught_crashes.txt
code 实现:
public class BLog {
public static final String CAUGHTEXCEPTIONPATH = CRASH_FILE_DIR_PATH + "caught_crashes.txt";
public static int e(String tag, String msg, Throwable tr) {
//把时间信息, 设备信息, 连同exception的stacktrace内容, 追加到"caught_crashes.txt"文件的末尾.
saveLogInfoToFile(CAUGHTEXCEPTIONPATH, msg, tr);
return android.util.Log.e(tag, msg, tr);
}
}
良好的编码习惯是在每个try{}catch(Exception e){}中, 都调用BLog自定义的e(String tag, String msg, Throwable tr)把异常信息保存到本地,方便记录异常情况.
---DONE---
网友评论