Java Exception Format formatException(Throwable exception)

Here you can find the source of formatException(Throwable exception)

Description

Formatea una excepcion y las excepciones padre.

License

Apache License

Parameter

Parameter Description
exception excepcion

Return

el fomato requerido

Declaration

public static String formatException(Throwable exception) 

Method Source Code

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

public class Main {
    /**/*from ww w  . j  av a2 s. co  m*/
     * Formatea una excepcion y las excepciones padre.
     * 
     * @param exception
     *            excepcion
     * @return el fomato requerido
     */
    public static String formatException(Throwable exception) {
        StringBuilder buffer = new StringBuilder();

        formatException(exception, buffer);

        return buffer.toString();
    }

    /**
     * formatela la excepcion.
     * 
     * @param exception excepcion
     * @param buffer bufer
     * @return StringBuilder
     */
    public static StringBuilder formatException(Throwable exception, StringBuilder buffer) {

        //         ByteArrayOutputStream escribe = new ByteArrayOutputStream();
        //         PrintStream print = new PrintStream(escribe);
        //         exception.printStackTrace(print);
        //         String prpr= escribe.toString();
        //   

        StringBuilder ret;

        if (exception == null) {
            ret = buffer;

        } else {
            buffer.append(exception.getClass().getName());
            buffer.append(": \"");
            buffer.append(exception.getMessage());
            buffer.append("\" \n");

            StackTraceElement array[] = exception.getStackTrace();

            for (StackTraceElement element : array) {
                buffer.append("\tat ");
                printStackTraceElement(element, buffer);
                buffer.append('\n');
            }

            if (exception.getCause() != null) {
                buffer.append("Parent exception: ");
                ret = formatException(exception.getCause(), buffer);
            } else {
                ret = buffer;
            }
        }
        return ret;
    }

    /**
     * IMprime un stacktrace element.
     * @param element el elemento a pintar
     * @param buffer el bufer donde pintar
     */
    public static void printStackTraceElement(StackTraceElement element, StringBuilder buffer) {
        buffer.append(element.getClassName());
        buffer.append('.');
        buffer.append(element.getMethodName());
        buffer.append('(');
        buffer.append(element.getFileName());
        if (element.getLineNumber() > 0) {
            buffer.append(':');
            buffer.append(element.getLineNumber());
        }
        buffer.append(')');
    }
}

Related

  1. fmtError( Exception e )
  2. formatException(final Exception e)
  3. formatException(Throwable ex)
  4. formatException(Throwable th)
  5. formatExceptionMsg(String msg)
  6. formatJavaException(Exception e)
  7. formatLogMessage(long id, Throwable exception)