Java Throwable to String getExceptionDetails (final Throwable e)

Here you can find the source of getExceptionDetails (final Throwable e)

Description

get Exception Details

License

Apache License

Declaration

public static String getExceptionDetails (final Throwable e)
    

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

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

import java.util.Arrays;

public class Main {
    public static String getExceptionDetails(final Throwable e) {
        return getExceptionDetails(e, "");
    }/*from  ww w  .j a v  a  2s . c  om*/

    public static String getExceptionDetails(final Throwable e,
            final String preamble) {
        return getExceptionDetails(e, preamble, true);
    }

    public static String getExceptionDetails(final Throwable e,
            final String preamble, final boolean includeContextId) {
        final StringBuilder result = new StringBuilder();
        final String stackTrace = getStackTrace(e);

        final String formatSpecifier = "%s%n%s";
        final String s = includeContextId ? contextFormatMessage(
                formatSpecifier, preamble, stackTrace) : String.format(
                formatSpecifier, preamble, stackTrace);
        result.append(s);

        // Remove trailing newline.
        return result.toString().trim();
    }

    public static String getStackTrace(final Throwable e) {
        final Writer sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);

        e.printStackTrace(pw);

        return sw.toString();
    }

    public static String contextFormatMessage(final String format,
            final Object... parameters) {
        return String.format(format, parameters);
    }

    /** Varargs wrapper */
    public static String toString(final Object... objects) {
        return Arrays.toString(objects);
    }
}

Related

  1. getDetails(Throwable t)
  2. getErrorInfo(Throwable error)
  3. getErrorMessage(Throwable e)
  4. getExceptionAsString(Throwable _ex)
  5. getExceptionDescription(Throwable e)
  6. getExceptionDetails(Throwable throwable)
  7. getExceptionHeadline(Throwable t)
  8. getExceptionPrintout(Throwable aThrowable)
  9. getExceptionStack(final Throwable e)