Java Exception Print printException(Throwable e, StackTraceElement[] trace)

Here you can find the source of printException(Throwable e, StackTraceElement[] trace)

Description

print Exception

License

EUPL

Declaration

private static void printException(Throwable e, StackTraceElement[] trace) 

Method Source Code

//package com.java2s;
/*/*www.jav a 2s  .c  o m*/
 * Copyright 2013-2014 SmartBear Software
 * Copyright 2014-2019 The TestFX Contributors
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the
 * European Commission - subsequent versions of the EUPL (the "Licence"); You may
 * not use this work except in compliance with the Licence.
 *
 * You may obtain a copy of the Licence at:
 * http://ec.europa.eu/idabc/eupl.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the Licence for the
 * specific language governing permissions and limitations under the Licence.
 */

public class Main {
    /**
     * If {@literal true} any exceptions encountered during execution of the
     * {@code async} methods will be printed to stderr.
     * The exceptions will be printed at the time they occur (not when fetched).
     */
    public static boolean printException = true;

    private static void printException(Throwable e, StackTraceElement[] trace) {
        StringBuilder out = new StringBuilder("--- Exception in Async Thread ---\n");
        out.append(e.getClass().getName()).append(": ").append(e.getMessage()).append('\n');
        StackTraceElement[] st = e.getStackTrace();
        out.append(printTrace(st));
        Throwable cause = e.getCause();
        while (cause != null) {
            out.append(cause.getClass().getName()).append(": ").append(cause.getMessage()).append('\n');
            st = cause.getStackTrace();
            out.append(printTrace(st));
            cause = cause.getCause();
        }
        if (trace != null) {
            out.append("--- Trace of caller of unhandled exception in Async Thread ---\n");
            out.append(printTrace(trace));
        }
        System.err.println(out.toString());
    }

    /**
     * Returns a {@code String} containing the printed stacktrace.
     *
     * @param st the stacktrace
     * @return a {@code String} containing the printed stacktrace
     */
    private static String printTrace(StackTraceElement[] st) {
        StringBuilder stackTrace = new StringBuilder();
        for (StackTraceElement ste : st) {
            stackTrace.append("\t").append(ste.toString()).append("\n");
        }
        return stackTrace.toString();
    }
}

Related

  1. printException(Exception e)
  2. printException(Exception e, String message)
  3. printException(String excption)
  4. printException(String prefix, Exception e)
  5. PrintException(StringBuilder Str, boolean HTML, Throwable T, boolean PrintCauseToo)
  6. printExceptionInfo(Exception e)
  7. printExceptionMessage(Throwable ex)
  8. PrintExceptionMessages(StringBuilder Str, boolean HTML, Throwable T, boolean PrintCauseToo)
  9. printExceptionWithStackTrace(Throwable t)