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()
.
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.access$600(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:
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.access$600(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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…