Java / Android - How to print out a full stack trace?
There's overrides of all the log methods with (String tag, String msg, Throwable tr)
signatures.
Passing an exception as the third parameter should give you the full stacktrace in logcat.
How can I get the current stack trace in Java?
You can use Thread.currentThread().getStackTrace()
.
That returns an array of StackTraceElement
s that represent the current stack trace of a program.
How to print stacktrace for an exception Android
} catch (IOException e) {
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}
And check the LogCat for the output.
Android - print full exception backtrace to log
try {
// code that might throw an exception
} catch (Exception e) {
Log.e("MYAPP", "exception", e);
}
More Explicitly with Further Info
(Since this is the oldest question about this.)
The three-argument Android log methods will print the stack trace for an Exception
that is provided as the third parameter. For example
Log.d(String tag, String msg, Throwable tr)
where tr
is the Exception.
According to this comment those Log methods "use the getStackTraceString()
method ... behind the scenes" to do that.
e.printStackTrace(); in string
Use the following piece of code:
Writer writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
String s = writer.toString();
There used to be a way to extract an exception stacktrace into the String in one line with Log.getStackTraceString
call. But starting from Android 4.0 (API 14) that method is not reliable anymore, as it returns an empty string for UnknownHostException
(see Android issue #21436 for the details, in short: "to reduce the amount of log spew that apps do in the non-error condition of the network being unavailable" Android engineers made IMHO a dubious decision to modify Log.getStackTraceString
method).
Thus it is better to use the code I provided at the beginning of this post.
Is there a way to see a complete stack trace from android?
Usually the "...N more" represents n more lines of the same error, anyway.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html#printStackTrace%28%29
Is there a way to print stack trace on demand?
This can be done in Java:
new Throwable().printStackTrace();
In Eclipse, if you create an "expression" with that code in the Expressions
view of Debug
perspective, it will print current stack trace (i.e. the stacktrace of the breakpoint your code stopped on) in the Console
view.
Related Topics
Capture and Log the Response Body
How to Limit the Number of Characters in Jtextfield
Regex Look-Behind Without Obvious Maximum Length in Java
Mapping Manytomany with Composite Primary Key and Annotation:
Preventing System.Exit() from API
Java: Why Do You Need to Specify an 'F' in a Float Literal
How to Implement in Java ( Jtextfield Class ) to Allow Entering Only Digits
How Returns Xxxsize from Jcomponent(S) Added to the Jlabel
Math.Random, Only Generating a 0
How to Scroll More Than One Object at the Same Time
How to Add Mouselistener to Item on Java Swing Canvas
Java Interfaces/Implementation Naming Convention
How to Make a New List in Java
How to Use Jndi Datasource Provided by Tomcat in Spring