Java Throwable to String getStackTrace(Throwable t)

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

Description

Returns a string representation of the stack trace for a Throwable object.

License

Open Source License

Parameter

Parameter Description
t The Throwable object of which the stack trace is desired.

Return

A String representation of the stack trace for t.

Declaration

public static String getStackTrace(Throwable t) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .j  a  v a2  s .c  o m*/
  MetaDB: A Distributed Metadata Collection Tool
  Copyright 2011, Lafayette College, Eric Luhrs, Haruki Yamaguchi, Long Ho.
    
  This file is part of MetaDB.
    
MetaDB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
MetaDB 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 MetaDB.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Returns a string representation of the stack trace 
     * for a Throwable object. 
     * @param t The Throwable object of which the stack trace is desired. 
     * @return A String representation of the stack trace for t.
     */
    public static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        String trace = sw.toString();
        int traceLength = trace.length();

        if (traceLength >= 1200)
            return trace.substring(0, 1199);
        else
            return trace.substring(0, traceLength - 1);

    }
}

Related

  1. getStackTrace(Throwable t)
  2. getStackTrace(Throwable t)
  3. getStackTrace(Throwable t)
  4. getStackTrace(Throwable t)
  5. getStackTrace(Throwable t)
  6. getStackTrace(Throwable t)
  7. getStackTrace(Throwable t)
  8. getStackTrace(Throwable t0)
  9. getStackTrace(Throwable tException)