如何从 throwable 中捕获异常的原因?
How to capture the cause of an exception from a throwable?
我有以下代码使用 google 分析来跟踪未捕获的异常:
ExceptionReporter myHandler =
new ExceptionReporter(AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP),
Thread.getDefaultUncaughtExceptionHandler(), this);
ExceptionParser ep = new ExceptionParser() {
@Override
public String getDescription(String s, Throwable throwable) {
return "UserID:" + userID + " Version:" + versionName + " Exception:" + stringifyStacktrace(null, throwable);
}
};
private String stringifyStacktrace (Exception e, Throwable t){
if(e != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
} else {
return t.toString() + Arrays.toString(t.getStackTrace());
}
}
问题是我只得到以下信息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.jiyofit.basic_app/in.jiyofit.basic_app.WorkoutActivity}: java.lang.ArithmeticException: divide by zero
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我想要给出异常行原因的堆栈跟踪的其余部分。我没有得到堆栈跟踪的以下部分:
Caused by: java.lang.ArithmeticException: divide by zero
at in.jiyofit.basic_app.WorkoutActivity.onCreate(WorkoutActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
如何获得这个?
从日志中尝试静态方法getStackTraceString() class
我有以下代码使用 google 分析来跟踪未捕获的异常:
ExceptionReporter myHandler =
new ExceptionReporter(AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP),
Thread.getDefaultUncaughtExceptionHandler(), this);
ExceptionParser ep = new ExceptionParser() {
@Override
public String getDescription(String s, Throwable throwable) {
return "UserID:" + userID + " Version:" + versionName + " Exception:" + stringifyStacktrace(null, throwable);
}
};
private String stringifyStacktrace (Exception e, Throwable t){
if(e != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
} else {
return t.toString() + Arrays.toString(t.getStackTrace());
}
}
问题是我只得到以下信息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.jiyofit.basic_app/in.jiyofit.basic_app.WorkoutActivity}: java.lang.ArithmeticException: divide by zero
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我想要给出异常行原因的堆栈跟踪的其余部分。我没有得到堆栈跟踪的以下部分:
Caused by: java.lang.ArithmeticException: divide by zero
at in.jiyofit.basic_app.WorkoutActivity.onCreate(WorkoutActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
如何获得这个?
从日志中尝试静态方法getStackTraceString() class