Java Throwable to String getStackTraceAsString(Throwable ex)

Here you can find the source of getStackTraceAsString(Throwable ex)

Description

get Stack Trace As String

License

Open Source License

Declaration

public static String getStackTraceAsString(Throwable ex) 

Method Source Code

//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();
    }
}

Related

  1. getStackTrace(Throwable thrown)
  2. getStackTraceAsByteArrayOutputStream(Throwable throwable)
  3. getStackTraceAsStr(Throwable t)
  4. getStackTraceAsString(Throwable e)
  5. getStackTraceAsString(Throwable ex)
  6. getStackTraceAsString(Throwable t)
  7. getStackTraceAsString(Throwable t)
  8. getStackTraceAsString(Throwable t)
  9. getStackTraceAsString(Throwable t)