Java Exception Print printExceptionMessage(Throwable ex)

Here you can find the source of printExceptionMessage(Throwable ex)

Description

Return stack trace of the Throwable ex.

License

Open Source License

Parameter

Parameter Description
ex The throwable to be print

Return

Stack trace of the throwable

Declaration

public static String printExceptionMessage(Throwable ex) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*  w  w  w .jav a2  s  .  c  o m*/
     * Return stack trace of the Throwable ex. This method will return first 5
     * lines of the trace
     * 
     * @param ex
     *            The throwable to be print
     * @return Stack trace of the throwable
     * @see printExceptionMessage(Throwable ex, int rowCount)
     */
    public static String printExceptionMessage(Throwable ex) {
        return printExceptionMessage(ex, 15);
    }

    /**
     * Return stack trace of the Throwable ex
     * 
     * @param ex
     *            The throwable to be print
     * @param rowCount
     *            # of lines that should return if -1 then return all stack
     *            trace
     * @return Stack trace of the throwable
     */
    public static String printExceptionMessage(Throwable ex, int rowCount) {
        String exMsg = ex.getLocalizedMessage();
        StringBuffer buf = new StringBuffer(ex.getClass().getName());
        buf.append(": ");
        if (!isNull(exMsg))
            buf.append(exMsg);
        buf.append(" \r\n");
        StackTraceElement[] trace = ex.getStackTrace();
        if (rowCount == -1)
            rowCount = trace.length;
        for (int i = 0; i < rowCount && i < trace.length; i++) {
            buf.append("\r\n        at ");
            buf.append(trace[i]);
        }
        return buf.toString();
    }

    public static final boolean isNull(Object str) {
        return (str == null || str.toString().trim().length() == 0);
    }
}

Related

  1. printException(String excption)
  2. printException(String prefix, Exception e)
  3. PrintException(StringBuilder Str, boolean HTML, Throwable T, boolean PrintCauseToo)
  4. printException(Throwable e, StackTraceElement[] trace)
  5. printExceptionInfo(Exception e)
  6. PrintExceptionMessages(StringBuilder Str, boolean HTML, Throwable T, boolean PrintCauseToo)
  7. printExceptionWithStackTrace(Throwable t)