Here you can find the source of getStackTraceAsString(Throwable ex)
public static String getStackTraceAsString(Throwable ex)
//package com.java2s; /*--------------------------------------------------------------------------* | Copyright (C) 2006 Christopher Kohlhaas | | | | This program is free software; you can redistribute it and/or modify | | it under the terms of the GNU General Public License as published by the | | Free Software Foundation. A copy of the license has been included with | | these distribution in the COPYING file, if not go to www.fsf.org | | | | As a special exception, you are granted the permissions to link this | | program with every library, which license fulfills the Open Source | | Definition as published by the Open Source Initiative (OSI). | *--------------------------------------------------------------------------*/ import java.io.ByteArrayOutputStream; import java.io.PrintWriter; public class Main { public static String getStackTraceAsString(Throwable ex) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(bytes, true); writer.println("<h2>" + ex.getMessage() + "</h2><br/>"); ex.printStackTrace(writer);/* w w w .j a va2 s . co m*/ while (true) { ex = ex.getCause(); if (ex != null) { writer.println("<br/><h2>Caused by: " + ex.getMessage() + "</h2><br/>"); ex.printStackTrace(writer); } else { break; } } return bytes.toString(); } }