Java Exception Print printException(Exception e)

Here you can find the source of printException(Exception e)

Description

print Exception

License

Open Source License

Declaration

public static void printException(Exception e) 

Method Source Code

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

public class Main {
    public static void printException(Exception e) {
        StackTraceElement ste = null;

        // this loop is important to find the proper stack trace element
        // without this, there may be null fileName and -1 lineNumber
        int i = 0;
        while (i < e.getStackTrace().length && (ste = e.getStackTrace()[i++]).getLineNumber() == -1)
            ;/*from   w  w  w  .ja v a  2 s  .  com*/
        if (ste == null) {
            printError("Utilities.printException error: StackTraceElement ste is null");
            return;
        }

        // set the respective variables
        String className = ste.getClassName();
        String methodName = ste.getMethodName();
        String fileName = ste.getFileName();
        int lineNumber = ste.getLineNumber();
        // print error message and location
        printError(e.toString());
        printError("\tat " + className + "." + methodName + "(" + fileName + ":" + lineNumber + ")");
    }

    public static void printError(String message) {
        System.err.println(message);
    }
}

Related

  1. printException(Exception e)
  2. printException(Exception e)
  3. printException(Exception e, String message)
  4. printException(String excption)
  5. printException(String prefix, Exception e)