Java Stacktrace Print printStackTrace(Exception e)

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

Description

print Stack Trace

License

Apache License

Declaration

public static void printStackTrace(Exception e) 

Method Source Code

//package com.java2s;
/**/* w  w w  . ja  v a2s. c om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

public class Main {
    private static PrintStream pps;

    public static void printStackTrace(Exception e) {
        if (e == null)
            print(getStackTrace());
        else
            print(stackTraceToString(e.getStackTrace()));
    }

    public static void printStackTrace() {
        printStackTrace(null);
    }

    public static void print(String msg) {
        print(pps, msg);
    }

    public static void print(PrintStream ps, String msg) {
        System.out.print(msg);
        if (ps != null) {
            ps.print(msg);
        }
    }

    public static String getStackTrace() {
        Thread t = Thread.currentThread();
        StackTraceElement[] ste = t.getStackTrace();
        return stackTraceToString(ste);
    }

    public static String stackTraceToString(StackTraceElement[] ste) {
        String str = "";
        for (int i = 0; i < ste.length; i++) {
            str += String.format("    [%02d] %s \n", i, ste[i].toString());
        }
        return str;
    }
}

Related

  1. printStackTrace()
  2. printStackTrace()
  3. printStackTrace(Exception e)
  4. printStackTrace(Exception e)
  5. printStackTrace(Exception e)
  6. printStackTrace(Exception exception)
  7. printStackTrace(final Exception exception)
  8. printStackTrace(final StackTraceElement[] stack)
  9. printStackTrace(IOException ioe)