Retrieves a single-line string representation of the stack trace for the provided Throwable . - Java java.lang

Java examples for java.lang:Throwable

Description

Retrieves a single-line string representation of the stack trace for the provided Throwable .

Demo Code

/*/*from w  w  w  .  jav a2  s .  c o  m*/
 * Copyright 2011-2016 UnboundID Corp.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 */
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main{
    /**
     * Retrieves a single-line string representation of the stack trace for the
     * provided {@code Throwable}.  It will include the unqualified name of the
     * {@code Throwable} class, a list of source files and line numbers (if
     * available) for the stack trace, and will also include the stack trace for
     * the cause (if present).
     *
     * @param  t  The {@code Throwable} for which to retrieve the stack trace.
     *
     * @return  A single-line string representation of the stack trace for the
     *          provided {@code Throwable}.
     */
    public static String getStackTrace(final Throwable t) {
        final StringBuilder buffer = new StringBuilder();
        getStackTrace(t, buffer);
        return buffer.toString();
    }
    /**
     * Appends a single-line string representation of the stack trace for the
     * provided {@code Throwable} to the given buffer.  It will include the
     * unqualified name of the {@code Throwable} class, a list of source files and
     * line numbers (if available) for the stack trace, and will also include the
     * stack trace for the cause (if present).
     *
     * @param  t       The {@code Throwable} for which to retrieve the stack
     *                 trace.
     * @param  buffer  The buffer to which the information should be appended.
     */
    public static void getStackTrace(final Throwable t,
            final StringBuilder buffer) {
        buffer.append(t.getClass().getSimpleName());
        buffer.append('(');

        final String message = t.getMessage();
        if (message != null) {
            buffer.append("message='");
            buffer.append(message);
            buffer.append("', ");
        }

        buffer.append("trace='");
        getStackTrace(t.getStackTrace(), buffer);
        buffer.append('\'');

        final Throwable cause = t.getCause();
        if (cause != null) {
            buffer.append(", cause=");
            getStackTrace(cause, buffer);
        }
        buffer.append(", revision=");
        buffer.append(Version.REVISION_ID);
        buffer.append(')');
    }
    /**
     * Returns a single-line string representation of the stack trace.  It will
     * include a list of source files and line numbers (if available) for the
     * stack trace.
     *
     * @param  elements  The stack trace.
     *
     * @return  A single-line string representation of the stack trace.
     */
    public static String getStackTrace(final StackTraceElement[] elements) {
        final StringBuilder buffer = new StringBuilder();
        getStackTrace(elements, buffer);
        return buffer.toString();
    }
    /**
     * Appends a single-line string representation of the stack trace to the given
     * buffer.  It will include a list of source files and line numbers
     * (if available) for the stack trace.
     *
     * @param  elements  The stack trace.
     * @param  buffer  The buffer to which the information should be appended.
     */
    public static void getStackTrace(final StackTraceElement[] elements,
            final StringBuilder buffer) {
        for (int i = 0; i < elements.length; i++) {
            if (i > 0) {
                buffer.append(" / ");
            }

            buffer.append(elements[i].getMethodName());
            buffer.append('(');
            buffer.append(elements[i].getFileName());

            final int lineNumber = elements[i].getLineNumber();
            if (lineNumber > 0) {
                buffer.append(':');
                buffer.append(lineNumber);
            }
            buffer.append(')');
        }
    }
}

Related Tutorials