Java Exception to String exceptionFormat(E exception)

Here you can find the source of exceptionFormat(E exception)

Description

exception Format

License

Open Source License

Declaration

public static <E> String exceptionFormat(E exception) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static <E> String exceptionFormat(E exception) {

        Exception e = (Exception) exception;
        Integer line = null;//from  w  w w.  j av  a 2s .c o  m
        String className = null;
        String methodName = null;

        if (e.getStackTrace() != null && e.getStackTrace().length > 2) {
            line = e.getStackTrace()[2].getLineNumber();
            className = e.getStackTrace()[2].getClassName();
            methodName = e.getStackTrace()[2].getMethodName();
        }
        if (line == null) {
            line = Thread.currentThread().getStackTrace()[2].getLineNumber();
        }
        if (className == null) {
            className = Thread.currentThread().getStackTrace()[2].getClassName();
        }
        if (methodName == null) {
            methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        }

        return String.format("The msg: %s," + "The Class: %s," + "The Method: %s," + "The Line: %d,",
                e.getMessage(), className, methodName, line);

    }
}

Related

  1. exception2string(Throwable exception)
  2. exception2StringShort(Exception e)
  3. exception_details(Exception e)
  4. exception_details(Exception e, String message)
  5. exceptionStackToString(Throwable x)
  6. exceptionToString(final Exception e)
  7. exceptionToString(final Throwable ex)
  8. exceptionToString(final Throwable throwable)