Put printStackTrace() into a StringTag(s): Language
public class TestException { public static void main(String args[]) { try { throw new Exception("for no reason!"); } catch (Exception e) { e.printStackTrace(); } } // output : // java.lang.Exception: for no reason! // at TestException.main(TestException.java:8) }
You can redirect the StackTrace to a String with a StringWriter/PrintWriter :
import java.io.PrintWriter; import java.io.StringWriter; public class TestException { public static void main(String args[]) { try { throw new Exception("for no reason!"); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); System.out.println(sw.toString().toUpperCase()); } } // output : // JAVA.LANG.EXCEPTION: FOR NO REASON! // AT TESTEXCEPTION.MAIN(TESTEXCEPTION.JAVA:7) }
This can be useful if you want to format the StackTrace before showing it to the user.