import java.io.PrintWriter; import java.io.StringWriter; public class Test { public static void main(String[] args) { try{ //some code }catch(Exception e){ StringWriter stringWriter = new StringWriter(); e.printStackTrace(new PrintWriter(stringWriter)); String exceptionString = stringWriter.toString(); System.out.println(exceptionString); } } }
參考: