Java Throwable to String getExceptionStackTrace(Throwable exception)

Here you can find the source of getExceptionStackTrace(Throwable exception)

Description

Obtains the entire stracktrace of an exception and converts it into a string.

License

CDDL license

Parameter

Parameter Description
exception the exception whose stacktrace has to be converted

Return

the stracktrace, converted into a string

Declaration

public static String getExceptionStackTrace(Throwable exception) 

Method Source Code


//package com.java2s;
/*//from  w w w  . ja  v a 2 s  .  co m
 * Copyright 2001-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 * $Id: ExceptionUtils.java 3108 2006-03-13 18:03:00Z gbevin $
 */

import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    /**
     * Obtains the entire stracktrace of an exception and converts it into a
     * string.
     * 
     * @param exception the exception whose stacktrace has to be converted
     * @return the stracktrace, converted into a string
     * @since 1.0
     */
    public static String getExceptionStackTrace(Throwable exception) {
        if (null == exception)
            throw new IllegalArgumentException("exception can't be null;");

        String stack_trace = null;

        StringWriter string_writer = new StringWriter();
        PrintWriter print_writer = new PrintWriter(string_writer);

        exception.printStackTrace(print_writer);

        stack_trace = string_writer.getBuffer().toString();

        print_writer.close();

        try {
            string_writer.close();
        }
        // JDK 1.2.2 compatibility
        catch (Throwable e2) {
        }

        return stack_trace;
    }
}

Related

  1. getExceptionHeadline(Throwable t)
  2. getExceptionPrintout(Throwable aThrowable)
  3. getExceptionStack(final Throwable e)
  4. getExceptionStack(Throwable e)
  5. getExceptionStackTrace(Throwable ex)
  6. getExceptionStackTrace(Throwable exception)
  7. getExceptionString(Throwable e)
  8. getExceptionString(Throwable t)
  9. getExceptionString(Throwable throwable)