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?
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.
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.
Just use new Throwable(). printStackTrace() method and it will print complete stack trace from where a method is called, into the console.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With