40

How do I get full exception message in Java?

I am creating a Java Application. If there is a runtime exception, a JDialog with a JTextArea will pop up. I tried to make a runtime exception in my application, but the text area showing the exception message looks like this:

java.lang.ClassNotFoundException: com.app.Application

However, I want my text area to show something like:

enter image description here

Here is some part of my code surrounded by the try and catch:

String ex = e.toString();
this.addText(ex);

I've tried to use e.getLocalizedMessage() and e.getMessage(), but neither of these work.

3
  • Check the updated answer that I have provided. You can get trace converted to string format for you to display. Commented Aug 31, 2013 at 10:07
  • 1
    use ExceptionObject.printStackTrace(); Commented Aug 31, 2013 at 11:34
  • Just the stack trace to string part: stackoverflow.com/questions/1149703/… Commented Mar 19, 2015 at 13:22

7 Answers 7

88

You need to call the Throwable#printStackTrace(PrintWriter);

try{

}catch(Exception ex){
    String message = getStackTrace(ex);
}

public static String getStackTrace(final Throwable throwable) {
     final StringWriter sw = new StringWriter();
     final PrintWriter pw = new PrintWriter(sw, true);
     throwable.printStackTrace(pw);
     return sw.getBuffer().toString();
}

You can also use commons-lang-2.2.jar Apache Commons Lang library which provides this functionality:

public static String getStackTrace(Throwable throwable)
Gets the stack trace from a Throwable as a String.

ExceptionUtils#getStackTrace()

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

1 Comment

Awesome! Helped me in a InputStream error in Android :)
6

if you are not using any logger(Log4j or oracle logger api) then you can use the below statement to print the full exception message

try{
       // statement which you want to monitor 

}catch(Exception e){
    e.printStackTrace()
    // OR System.err.println(e.getStackTrace());
    // OR System.err.println(e);
    // OR System.err.println(e.getMessage());
    // OR System.err.println(e.getCause());
}

if you are using the Logger API then use the below statement

try{

         // statement which you want to monitor 

}catch(Exception e){
  log.error(e,e); 
}

Comments

6

To get the stack trace into a string you can use these lines:

    CharArrayWriter cw = new CharArrayWriter();
    PrintWriter w = new PrintWriter(cw);
    e.printStackTrace(w);
    w.close();
    String trace = cw.toString();

Comments

3
public static String getStackMsg(Throwable e) {
    StringBuffer sb = new StringBuffer();
    sb.append(e.toString()).append("\n");
    StackTraceElement[] stackArray = e.getStackTrace();

    for(int i = 0; i < stackArray.length; ++i) {
        StackTraceElement element = stackArray[i];
        sb.append(element.toString() + "\n");
    }

    return sb.toString();
}

Comments

1

Try e.printStackTrace(), it will print trace for your exception with lines mentioned

Comments

1

e.printStackTrace will give the full stack trace

Comments

1

This is the full exception message.

If you want more information try e.printStackTrace()

then you will get excatly what you see on the picture you postet

Comments

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.