2

I want to track all type of exceptions in my Android project avoiding manual collecting exceptions from every catch block.

Does android have any such global interface that can capture information of every exception of application? I would like to track every exception in a single line.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Akshay kumar
  • 111
  • 5

3 Answers3

-1

If you are okay with the app crashing on every exception, you can set a handler for uncaught exceptions on the current Thread like this:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            // your exception handling code here
        }
});

If you use AsyncTasks or other Threading methods in your app, you have to do this on every new thread.

This method is normally used to write your own error reporting, maybe it's not exactly what you want to do.

If you don't want that your app crashes, I think you have to implement something on your own that is easily callable from within catch blocks.

tknell
  • 9,007
  • 3
  • 24
  • 28
-1

Not that I know of. If there was though, I'm sure no one would be using try catch blocks, and it would be a well known process/method. You can, though, debug with android studio.

Only thing I can think of that is similar could be setting a common handler to catch exceptions:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

And the method that goes with that annonymous inner class is:

        public void uncaughtException(Thread thread, Throwable throwable) {

Where you put your exceptions. I found an example on tutorialspoint.com, and edited a screen shot to make it clearer:

enter image description here

Then, you can notice that we purposely throw that runTimeException after starting the thread. So, result will be:

 Thread[Thread-0,5,main] throws exception: java.lang.RuntimeException
user229044
  • 232,980
  • 40
  • 330
  • 338
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
-1

Is it android have any such global interface who capture information of every exception of application,so that i would track every exception in a single line?

No there is not.

Exceptions are either checked or unchecked. You 'must' catch checked-exceptions using try-catch or throws unless the code wont compile.

Any uncaught exception is handled in unCoughtExceptionHandler object of the Thread which exception uccored in.

Hojjat Imani
  • 333
  • 1
  • 15