Java Throwable to String getStackTraceAsStr(Throwable t)

Here you can find the source of getStackTraceAsStr(Throwable t)

Description

Returns the stack trace of t as a string with each element of the trace separated by a newline character.

License

Open Source License

Parameter

Parameter Description
t <code>Throwable</code> object

Return

String form of full stack trace of t

Declaration

public static String getStackTraceAsStr(Throwable t) 

Method Source Code


//package com.java2s;
/*/*from   w  ww  . ja  v  a2 s.  co  m*/
 * Copyright (c) 2013 Game Salutes.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Contributors:
 *     Game Salutes - Repackaging and modifications of original work under University of Chicago and Apache License 2.0 shown below
 * 
 * Repackaging from edu.uchicago.nsit.iteco.utils to com.gamesalutes.utils
 * 
 * Copyright 2008 - 2011 University of Chicago
 * 
 * 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.PrintWriter;
import java.io.StringWriter;

public class Main {
    /**
     * Returns the stack trace of <code>t</code> as a string with each
     * element of the trace separated by a newline character.
     * 
     * @param t <code>Throwable</code> object
     * @return String form of full stack trace of <code>t</code>
     */
    public static String getStackTraceAsStr(Throwable t) {
        //      StringBuilder str = new StringBuilder();
        //      // get all causes
        //      int count = 0;
        //      do
        //      {
        //         if(++count > 1)
        //            str.append("Caused by: ");
        //         str.append(t);
        //         str.append("\n");
        //         str.append(getStackTraceAsStr(t.getStackTrace(),true));      
        //      }
        //      while((t = t.getCause()) != null);
        //      
        //      return str.toString();

        StringWriter sw = new StringWriter(2048);
        PrintWriter writer = new PrintWriter(sw);
        t.printStackTrace(writer);
        writer.flush();
        return sw.toString();

    }

    /**
     * Returns the stack trace as a string with each
     * element of the trace separated by a newline character.
     * 
     * @param ste the stack trace
     * @param indent <code>true</code> to tab each stack trace element
     * @return String form of full stack trace
     */
    public static String getStackTraceAsStr(StackTraceElement[] ste, boolean indent) {
        StringBuilder str = new StringBuilder(256);

        for (StackTraceElement e : ste) {
            if (indent)
                str.append("\t");
            str.append(e);
            str.append("\n");
        }
        return str.toString();
    }
}

Related

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