Java Throwable to String getStackTraceAsString(Throwable throwable)

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

Description

get Stack Trace As String

License

Apache License

Declaration

public static String getStackTraceAsString(Throwable throwable) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    static final String HEXES = "0123456789abcdef";

    public static String getStackTraceAsString(Throwable throwable) {
        StringWriter stringWriter = new StringWriter();
        throwable.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }/*from   www . j  a va2s . c om*/

    /**
     * Try and provide some nice formatting for this object
     */
    private static String toString(Object fieldValue) {
        String toString;

        if (fieldValue instanceof byte[]) {
            toString = toHex((byte[]) fieldValue);
        } else {
            toString = fieldValue.toString();
        }

        return toString;
    }

    public static String toHex(byte[] raw) {
        if (raw == null) {
            return null;
        }
        int index = 0;
        final StringBuilder hex = new StringBuilder(2 * raw.length);
        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));

            if (index < raw.length - 1) {
                hex.append(" ");
            }

            index++;
        }
        return hex.toString();
    }
}

Related

  1. getStackTraceAsString(Throwable ex)
  2. getStackTraceAsString(Throwable t)
  3. getStackTraceAsString(Throwable t)
  4. getStackTraceAsString(Throwable t)
  5. getStackTraceAsString(Throwable t)
  6. getStackTraceAsString(Throwable throwable)
  7. getStackTraceAsText(Throwable t)
  8. getStackTraceFromBaseException(Throwable exc)
  9. getStackTraceFromThrowable(Throwable t)