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:
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With