Java Stacktrace Print printStackTrace()

Here you can find the source of printStackTrace()

Description

Print stack trace string.

License

Open Source License

Return

the string

Declaration

public static String printStackTrace() 

Method Source Code

//package com.java2s;
/*-/* w w w  .j  a  v a  2  s.c o  m*/
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

import java.io.IOException;

import java.io.PrintWriter;

import java.io.StringWriter;

public class Main {
    /**
     * Print stack trace string.
     *
     * @return the string
     */
    public static String printStackTrace() {

        StringWriter sw = new StringWriter();
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        for (StackTraceElement traceElement : trace) {
            sw.write("\t  " + traceElement);
            sw.write(System.lineSeparator());
        }
        String str = sw.toString();
        try {
            sw.close();
        } catch (IOException e0) {
            System.err.println(e0);
        }
        return str;

    }

    /**
     * Gets stack trace.
     *
     * @param t0 the t 0
     * @return the stack trace
     */
    public static String getStackTrace(Throwable t0) {
        if (null == t0) {
            return "";
        }
        StringWriter sw = new StringWriter();
        t0.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }
}

Related

  1. printStackTrace()
  2. printStackTrace()
  3. printStackTrace()
  4. printStackTrace()
  5. printStackTrace()
  6. printStackTrace()
  7. printStackTrace()
  8. printStackTrace()
  9. printStackTrace()