Java Stacktrace to String getStackTrace(Exception e)

Here you can find the source of getStackTrace(Exception e)

Description

get Stack Trace

License

Open Source License

Declaration

public static String getStackTrace(Exception e) 

Method Source Code


//package com.java2s;
/*/*from   w w  w .  j  av  a  2  s .  c om*/
 * Static String formatting and query routines.
 * Copyright (C) 2001,2002 Stephen Ostermiller <utils@Ostermiller.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * See COPYING.TXT for details.
 */

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

public class Main {
    public static String getStackTrace(Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        String ret = sw.toString();
        pw.close();
        try {
            sw.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }

    public static String getStackTrace(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        String ret = sw.toString();
        pw.close();
        try {
            sw.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }
}

Related

  1. getStackTrace()
  2. getStackTrace()
  3. getStackTrace(Exception e)
  4. getStackTrace(Exception e)
  5. getStackTrace(Exception e)
  6. getStackTrace(Exception exception)
  7. getStackTrace(Exception x)
  8. getStacktraceAsString(Exception e)