Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

android - How can I get the line number where the exception was thrown using Thread.UncaughtExceptionHandler?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...