Even explicitly writing e.printStackTrace() it doesn't print to the console, why?
Checkout Logcat in DDMS perspective it should be there not in the Console tab.
Or use the ddms command tool to read logcat.
printStackTrace() doesn't print to the console, it prints to the standard error stream. If you want to print to the screen, set your display text to e.getMessage() or e.getStackTrace(). (Although I do recommend learning to debug with logcat instead.)
use Log.X() where X is the type of error console you want(Log.e, Log.v, Log.d, etc.)
You cannot print to console on Android. You have to use Android Log class. When I have an exception and I want to print it I'm using:
for (StackTraceElement element : exception.getStackTrace())
Log.e("my_tag", element.toString());
or you can try
String stackTrace = Log.getStackTraceString(throwable);
And after that you can print it to Android Log.
Related
In regards to Log4j v2, I'd like to call log.error(exceptionTypeObj) and have the stack trace printed to my logfile instead of exceptionTypeObj.toString(). Is that possible?
I'd rather write code like log.error(e) and be done with it.
It's error prone and redundant for me to keep writing something like log.error("error", exceptionTypeObj).
The String parameter is redundant because you are not using it for the correct purpose. An Exception will tell you what error occurred but it won't tell you what you were trying to do when it happened. That is why you should always provide the string. Provide a String that says something like "Error occurred while attempting to update the user profile for user {}", and then provide the user name or id.
Without the String parameter you are forcing your support staff to call engineering to find out what the exception means and what they should do about it.
Anyone know why Exception().getMessage return to 1? What does it mean? I have tried to find the answer but nothing found, thank.
Use e.printStackTrace instead of e.getMessage() in case of exception. It will display all the information required to debug the issue, whereas printing only the message of the exception doesn't tell you almost anything.
If using a logger, use the form that accepts an exception/throwable, usually of the form logger.error("Custom error message", e);, and the stacktrace will be printed.
Is it possible to get the whole trace from an exception without using printStackTrace()?.
One of the reasons it's because I use CheckStyle and I'm getting an ugly message. It's not enough just to call toString(). I want to get the complete message, because I needed to send me by email.
Currently, I'm doing
StringWriter e = new StringWriter();
exception.printStackTrace(new PrintWriter(e));
--Avoid tracing with printStackTrace(), use log4j instead.
Otherwise, I could disable CheckStyle there.
If you want to get error details try Apache Commons Lang api
There is a class ExceptionUtils . You can use getFullStackTrace
Have you tried?:
Throwable thr = e.getStackTrace();
StackTraceElement[] lines = thr.getStackTrace();
And working from there. Also, take a look at the javadocs Oli linked above
You can get the stack trace from the current thread,
check this :
Thread.currentThread().getStackTrace()
Try this, you will be able to print stack trace with no check style error.
import org.apache.commons.lang.exception.ExceptionUtils;
String errorMessage = ExceptionUtils.getFullStackTrace(exception);
logger.debug(errorMessage);
Here is my code for Xcode and iOS:
NSLog(#"EAN-8: %d", finalNumber);
How do I do the same thing on Java so it appears in the LogCat?
Thanks.
Log.d("MyApplication", String.format("EAN-8: %d", finalNumber));
The first parameter is called the tag, which you can use to filter Logcat output if needed. See the docs for more info/log severities.
This is probably a newbie question, but hope you can help me. :) I have something like this:
try
{
//try to do something there
}
catch (IOException e)
{
//handle the exception
e.printStackTrace();
}
I am using NetBeans IDE and for some reason the printStackTrace is underlined in a squiggly line. When I press Alt+Enter, it says Throwable.printStackTrace() should be removed. What does this mean? Could anyone give more insight as what this may mean? Or can I ignore this?
Thanks!
Try:
e.printStackTrace(System.out);
It is just a recommendation. In eclipse it is fine - I believe it is just the IDE telling you that there are more conventional methods of doing it, like some of the other answers.
I find that it is useful for debugging, and that you should tell users when a fatal error is going to occur, to use a debug mode (like a console switch -d) to collect these logs.
It's probably because printStackTrace() doesn't really handle the error as much as it just dumps the stack in the console. It acts as a placeholder until you replace it with proper error handling (if it is needed at all) and replace the output with a logger of some sort.
e.printStackTrace();
Is not good practice because it prints in the default ErrorStream, which most of the times is the console!
NetBeans should be warning you about that. The good practice about it, is logging the message. Follow same reference:
http://onjava.com/pub/a/onjava/2003/11/19/exceptions.html
EDIT
See first comment bellow to more info.
Just printing a stack trace is not enough. Printing the exception's stack trace in itself doesn't mean that it is completely bad practice, but printing only the stack trace when an exception occurs is an issue.
Always log exceptions(using a good logging framework), but do not expose them to the end-user. And keep ensure that showing stack traces only in development mode.
I myself use(most of the time) logger.log(Level.SEVERE, <exception>.getMessage(), <exception>);.
when netbeans suggest you to handle the exception 'Surround Statement with try-catch', if you click on this, it will generate):
try {
//something need to be handle(exception that throw)
} catch (SQLException ex) {
Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);
}
Which is better than ex.printStackTrace();.
These may help:
Best Practices for Exception Handling
Javarevisited- logging's.
What are the latest options in Java logging frameworks?
Benchmarking Java logging frameworks.