1

Is there a way I can automatically print a stack trace when Exception occurs? I understand I can do so by manually surrounding a block with a try-catch statement, but there's no prior-knowledge where an exception would happen in a program, and doing it in suspicious region block by block would be super inefficient, since there're potentially many. So is there any configuration option or any programmatic way to do so in Android?(like surrounding a try-catch block in the highest level of method?, but what's the highest level of method)

4 Answers 4

3

Define a global exception handler in your Application class and add any code you need to it.

Thread.setDefaultUncaughtExceptionHandler(
                new DefaultExceptionHandler(this));

It is recommended that you still re-throw caught exceptions so that the app stops running, because it will be in an unstable state and not doing so can cause the app to freeze up.

Sign up to request clarification or add additional context in comments.

3 Comments

Another followup question. How can I set it such that any child thread s derived from this parent thread will use the same exception handler?
Android apps run on a single thread (called the UI thread), so this handler will only catch exceptions on the main thread. Any child threads are background threads started explicitly from your code (for example from an AsyncTask or a Service). The easiest way to handle global exceptions there would be to individually set an exception handler on each of these threads, perhaps by defining a reusable component and extending from it.
alright, but this still involves manual work. Anyway, it's good enough. Big thanks @Nachi
0
catch (Exception e) {e.printStackTrace();}

The highest level is the Application level I believe. But in general just catch it at the usual activity lifecycle should be sufficient.

1 Comment

of course I know that, question is where to put it, once for all solves every exception
0

Extend Exception and do whatever you want in your custom Exception class.

1 Comment

I don't get it. the effect I wanted to achieve is to place only on try-block and print the stack trace, So I have a direct sense which part of the program was wrong. I don't think customized exception would do the trick
0

There are two major kinds of exceptions:

  1. Exceptions caused by programming errors (e.g. dereferencing a null reference, ...)
  2. Exceptions caused by the environment (e.g. user enters 'abc' in a numeric field, network cable was unplugged, ...)

In most of the cases you can't really recover from the first ones. (If something unexpectedly went wrong, how can you know what to do to recover safely?) This are usually unchecked exceptions and you don't have to catch them somewhere.

Uncaught exceptions are catched by the system/framework and logged automatically!

Take care for the second type of exceptions! They are mostly not unchecked and have to be either cached or specified via throws clause. Handle them, probably log them, and try to recover if possible.

This document explains how you can read the error log and other logs on android devices. There are also apps available that can display the log on the device itself.

Also read this basic exception tutorial!

7 Comments

thanks for the response. I understood logcat, but it's not enough. Logcat would only show brief instrumentation when runtime exception occurs (like thread 1 exited with uncaught exception). It will only be possible to have an idea of which line crashes by printing out the entire stack trace. I am interested in whether there exists a technique that can print the entire stack trace when a runtime exception happens. I have no intention of recovering from it. I just need a piece of debugging information telling me which line causes the problem
But the log files DOES contain full stack traces for uncaught exceptions!
Really? does the content of log file differ from those in log cat? 'cause in logcat, there isn't much useful about runtime exception. I shall probably look into log files
I'm using the log view of android studio. There you can see the stack traces. And they are also contained when you load the log via 'adb logcat' from the device (which should actually be the same).
adb logcat should be the same as logcat in Android ADT, but I don't see a complete stack trace if runtime exception occurs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.