Java Stacktrace Print printStackTraceToString(Throwable aThrowable)

Here you can find the source of printStackTraceToString(Throwable aThrowable)

Description

Return a String containing the printed stacktrace of an exception.

License

Open Source License

Declaration

public static String printStackTraceToString(Throwable aThrowable) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.// w w w.  ja  v a2  s  .c  o  m
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 *     dminsky - added countOccurrencesOf(Object, List) API
 *     08/23/2010-2.2 Michael O'Brien
 *        - 323043: application.xml module ordering may cause weaving not to occur causing an NPE.
 *                       warn if expected "_persistence_*_vh" method not found
 *                       instead of throwing NPE during deploy validation.
 ******************************************************************************/

import java.io.Closeable;

import java.io.IOException;
import java.io.PrintWriter;

import java.io.StringWriter;

public class Main {
    /**
     * Return a String containing the printed stacktrace of an exception.
     */
    public static String printStackTraceToString(Throwable aThrowable) {
        StringWriter swriter = new StringWriter();
        PrintWriter writer = new PrintWriter(swriter, true);
        aThrowable.printStackTrace(writer);
        writer.close();
        return swriter.toString();
    }

    /**
     * Close a closeable object, eating the exception
     */
    public static void close(Closeable c) {
        try {
            if (c != null) {
                c.close();
            }
        } catch (IOException exception) {
        }
    }
}

Related

  1. printStackTraceElement(StackTraceElement element, StringBuilder buffer)
  2. printStackTraceHTML(Throwable e)
  3. printStackTraces()
  4. printStackTraceToOutputStream(Throwable t, OutputStream out)
  5. printStackTraceToString(final Throwable t)
  6. printStackTraceUsingStream(Throwable throwable)