Android 如何获取使用 Thread.UncaughtExceptionHandler 抛出异常的行号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23903831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the line number where the exception was thrown using Thread.UncaughtExceptionHandler?
提问by u1070512
When I use a try-catch block to catch an exception, I can get the line number where the exception was thrown by calling e.getStackTrace()
.
当我使用 try-catch 块捕获异常时,我可以通过调用e.getStackTrace()
.
Like this:
像这样:
java.lang.NumberFormatException: Invalid int: "abc"
java.lang.Integer.invalidInt(Integer.java:138)
java.lang.Integer.parse(Integer.java:375)
java.lang.Integer.parseInt(Integer.java:366)
java.lang.Integer.parseInt(Integer.java:332)
com.example.getarea.MainActivity.onCreate(MainActivity.java:42)
android.app.Activity.performCreate(Activity.java:5066)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
android.app.ActivityThread.access0(ActivityThread.java:151)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:155)
android.app.ActivityThread.main(ActivityThread.java:5536)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
dalvik.system.NativeStart.main(Native Method)
But when I use Thread.UncaughtExceptionHandler, calling e.getStackTrace()
give me this:
但是当我使用 Thread.UncaughtExceptionHandler 时,调用e.getStackTrace()
给我这个:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.getarea/com.example.getarea.MainActivity}: java.lang.NumberFormatException: Invalid int: "abc"
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
android.app.ActivityThread.access0(ActivityThread.java:151)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:155)
android.app.ActivityThread.main(ActivityThread.java:5536)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
dalvik.system.NativeStart.main(Native Method)
How can I get the line number where the exception was thrown using Thread.UncaughtExceptionHandler?
如何获取使用 Thread.UncaughtExceptionHandler 抛出异常的行号?
回答by Thilo
Try
尝试
e.getStackTrace()[0].getLineNumber();
The first element in the stack trace is the most recently called method (the top of the call stack).
堆栈跟踪中的第一个元素是最近调用的方法(调用堆栈的顶部)。
There are also methods to get the class, method and file name.
还有获取类、方法和文件名的方法。
If the e
you get in the UncaughtExceptionHandler does not work well for you (because it wraps the real exception), you can try e.getCause()
and look at that stack trace.
如果e
您在 UncaughtExceptionHandler 中获得的结果对您不起作用(因为它包含了真正的异常),您可以尝试e.getCause()
查看该堆栈跟踪。
回答by Martin Pfeffer
Inspired by Thilo and 范植勝 here the copy 'n paste version for the lazy people:
受到 Thilo 和范植胜的启发,这里为懒人提供了复制粘贴版本:
public class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
final Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Log.ee(thread.getClass().getName(),
throwable.getMessage(),
"Error in " + Arrays.toString(throwable.getCause().getStackTrace()));
if (uncaughtExceptionHandler != null) {
// let Android know what happened
uncaughtExceptionHandler.uncaughtException(thread, throwable);
} else {
// kill process
System.exit(-1);
}
}
}
Note: Log.ee is a custom file writer
注意:Log.ee 是一个自定义文件编写器
public static void ee(String tag, String msg, String msg2) {
try {
String ts = Utils.getReadableTimeStamp();
File dir = new File(Environment.getExternalStorageDirectory() + "/" + Const.CRASH_DIR);
dir.mkdirs();
PrintWriter printWriter = new PrintWriter(new FileWriter(new File(dir, "errors-" + sImei + ".log"), true));
printWriter.print("-------------------------------\r\n");
printWriter.print(ts + ":\r\n");
printWriter.print(tag + "\r\n");
printWriter.print(msg + "\r\n");
printWriter.print(msg2 + "\r\n");
printWriter.print("-------------------------------\r\n");
printWriter.flush();
} catch (IOException e) {
android.util.Log.e(Log.class.getName(), "ee -> IOException", e);
}
}