Java Throwable to String getStackTrace(final Throwable error)

Here you can find the source of getStackTrace(final Throwable error)

Description

Returns error stacktrace as a String.

License

Apache License

Parameter

Parameter Description
error error to be converted to String.

Return

error stacktrace

Declaration

public static String getStackTrace(final Throwable error) 

Method Source Code


//package com.java2s;
/*//from w ww.ja  v a 2  s.  c  o  m
 * ========================================================================
 *
 *  Codehaus CARGO, copyright 2004-2011 Vincent Massol, 2012-2018 Ali Tokmen.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  ========================================================================
 */

import java.io.IOException;
import java.io.StringWriter;

public class Main {
    /**
     * Returns error stacktrace as a String.
     * @param error error to be converted to String.
     * @return error stacktrace
     */
    public static String getStackTrace(final Throwable error) {
        StringWriter writer = new StringWriter();

        for (StackTraceElement element : error.getStackTrace()) {
            writer.write(element.toString());
        }

        try {
            return writer.toString();
        } finally {
            try {
                writer.close();
            } catch (IOException ex) {
                //nothing to do here.
            }
        }
    }
}

Related

  1. getExceptionTrace(Throwable e)
  2. getStackTrace (final Throwable e)
  3. getStackTrace(@Nonnull final Throwable throwable)
  4. getStackTrace(final Throwable aThrowable)
  5. getStackTrace(final Throwable e)
  6. getStackTrace(final Throwable exception)
  7. getStackTrace(final Throwable t)
  8. getStackTrace(final Throwable t)
  9. getStackTrace(final Throwable t)