如何打印异常Android的堆栈跟踪

时间:2011-11-21 19:37:04

标签: android android-logcat

我想打印堆栈跟踪,因为此刻我正在运行。

} catch (IOException e) {
    throw new Error("Copying Failed");
}

我被告知打印e.stacktrace();

我该怎么做?

5 个答案:

答案 0 :(得分:72)

} catch (IOException e) {
    Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}

并检查LogCat的输出。

答案 1 :(得分:25)

另一种非常有用的方法:

try
{
...
}
catch (Exception e)
{
    Log.e(APP_TAG, "STACKTRACE");
    Log.e(APP_TAG, Log.getStackTraceString(e));
}

答案 2 :(得分:13)

在Android中,您应该使用与Android使用的Logcat日志查看器完美配合的日志方法。

} catch (IOException e) {
   Log.e("YOUR ERROR TAG HERE", "Copying failed", e);
}

使用以throwable作为参数的Log.e方法,确保日志类将采用堆栈跟踪并将其正确记录到Logcat。如果你使用e.printStackTrace,这将使用一般的Java日志记录方法,它将无法在Logcat中正确显示,在某些情况下,无法双击logcat中的类名来跳转到提及的类和方法中。栈跟踪。

打印Stacktrace除以零将如下所示:

11-21 20:55:47.360: W/System.err(989): java.lang.ArithmeticException: divide by zero
11-21 20:55:47.379: W/System.err(989):  at test.tabs.TabChooser.onCreate(TabChooser.java:15)
11-21 20:55:47.390: W/System.err(989):  at android.app.Activity.performCreate(Activity.java:4465)
11-21 20:55:47.410: W/System.err(989):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-21 20:55:47.410: W/System.err(989):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-21 20:55:47.420: W/System.err(989):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 20:55:47.420: W/System.err(989):  at android.os.Looper.loop(Looper.java:137)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-21 20:55:47.430: W/System.err(989):  at java.lang.reflect.Method.invokeNative(Native Method)
11-21 20:55:47.430: W/System.err(989):  at java.lang.reflect.Method.invoke(Method.java:511)
11-21 20:55:47.430: W/System.err(989):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-21 20:55:47.430: W/System.err(989):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-21 20:55:47.430: W/System.err(989):  at dalvik.system.NativeStart.main(Native Method)

异常记录为警告,日志标记不是很有帮助。

将除数除以零的正确记录将如下所示:

11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356): Copying failed
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356): java.lang.ArithmeticException: divide by zero
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at test.tabs.TabChooser.onCreate(TabChooser.java:16)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.Activity.performCreate(Activity.java:4465)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.os.Looper.loop(Looper.java:137)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.main(ActivityThread.java:4340)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at java.lang.reflect.Method.invoke(Method.java:511)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at dalvik.system.NativeStart.main(Native Method)

使用您的日志标记和日志消息将异常正确记录为错误。

答案 3 :(得分:10)

} catch (IOException e) {
    e.printStackTrace();
}

答案 4 :(得分:1)

您很可能被要求通过e.printStackTrace(); ...

打印堆栈跟踪
} catch (IOException e) {
    e.printStackTrace();
    throw new Error("Copying Failed");
}