33

我想打印堆栈跟踪,因为目前我正在运行它。

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

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

我该怎么做呢?

4

5 回答 5

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

并检查 LogCat 的输出。

于 2011-11-21T19:42:27.923 回答
26

另一种方法,非常有用:

try
{
...
}
catch (Exception e)
{
    Log.e(APP_TAG, "STACKTRACE");
    Log.e(APP_TAG, Log.getStackTraceString(e));
}
于 2012-01-06T10:05:51.927 回答
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 中的类名来跳转到中提到的类和方法堆栈跟踪。

打印堆栈跟踪除以零将如下所示:

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)

异常被正确地记录为您的日志标签和日志消息的错误。

于 2011-11-21T19:42:33.460 回答
10
} catch (IOException e) {
    e.printStackTrace();
}
于 2011-11-21T19:39:33.917 回答
1

很可能您被要求通过e.printStackTrace();...打印堆栈跟踪

} catch (IOException e) {
    e.printStackTrace();
    throw new Error("Copying Failed");
}
于 2011-11-21T19:40:46.270 回答