Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

like image 586
范植勝 Avatar asked May 28 '14 05:05

范植勝


People also ask

How can I get the line number which threw exception?

Simple way, use the Exception. ToString() function, it will return the line after the exception description. You can also check the program debug database as it contains debug info/logs about the whole application.

Which is the exception thrown when a thread is?

An InterruptedException is thrown when a thread is interrupted while it's waiting, sleeping, or otherwise occupied. In other words, some code has called the interrupt() method on our thread. It's a checked exception, and many blocking operations in Java can throw it.

How do I print a stack trace?

Just use new Throwable(). printStackTrace() method and it will print complete stack trace from where a method is called, into the console.


1 Answers

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.

like image 109
Thilo Avatar answered Sep 23 '22 17:09

Thilo