Java Throwable to String getStackTraceAsString(Throwable t)

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

Description

get Stack Trace As String

License

Open Source License

Declaration

public static String getStackTraceAsString(Throwable t) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available at//from  w ww .j a  v a2 s. c om
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *******************************************************************************/

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

public class Main {
    public static final String EMPTY_STRING = "";

    public static String getStackTraceAsString(Throwable t) {
        if (t != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            t.printStackTrace(pw);
            final String stackTraceAsString = sw.toString();
            try {
                sw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            pw.close();
            return stackTraceAsString;
        } else {
            return EMPTY_STRING;
        }
    }
}

Related

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