0

I've implemented the following code to get crash details:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  @Override
  public void uncaughtException(Thread thread, Throwable ex) {
    Log.e(TAG, "Uncaught Exception!");
    StackTraceElement stackTraceElements[] = ex.getStackTrace();
  }
});

The problem I'm facing is, let's say two exceptions occur, RuntimeException which was caused by NullPointerException. In the above method, I'm getting trace for only RuntimeException. What should I do to get entire the back trace?

Swapnil Bhoite
  • 392
  • 2
  • 12

2 Answers2

1

Use getCause() method

ex.getCause().getStackTrace()
bedrin
  • 4,458
  • 32
  • 53
1

You can use the following.

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
sw.toString(); // stack trace as a string

This will give you the complete stack trace. For other ways of getting the same please go through this thread. Above has been copied from the same thread

Community
  • 1
  • 1
Jabir
  • 2,776
  • 1
  • 22
  • 31
  • This does work, but I wasn't looking for exact string, I wanted to format it my way and hence hence was looking for all `Throwable`s back traced. Thanks anyways! – Swapnil Bhoite Aug 10 '15 at 07:39