Example usage for java.lang Throwable printStackTrace

List of usage examples for java.lang Throwable printStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable printStackTrace.

Prototype

public void printStackTrace(PrintWriter s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print writer.

Usage

From source file:Utilities.java

/**
 * Return the stack trace from the passed exception as a string
 *
 * @param  th  The exception to retrieve stack trace for.
 *///from   w  w w.  j av a  2 s . c  o m
public static String getStackTrace(Throwable th) {
    if (th == null) {
        throw new IllegalArgumentException("Throwable == null");
    }

    StringWriter sw = new StringWriter();
    try {
        PrintWriter pw = new PrintWriter(sw);
        try {
            th.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    } finally {
        try {
            sw.close();
        } catch (IOException ex) {

        }
    }
}

From source file:Main.java

public static String toString(Throwable e) {
    StringWriter w = new StringWriter();
    PrintWriter p = new PrintWriter(w);
    p.print(e.getClass().getName() + ": ");
    if (e.getMessage() != null) {
        p.print(e.getMessage() + "\n");
    }/*  w  w  w  .ja  va  2s . co m*/
    p.println();
    try {
        e.printStackTrace(p);
        return w.toString();
    } finally {
        p.close();
    }
}

From source file:catalina.startup.BootstrapService.java

/**
 * Log a debugging detail message with an exception.
 *
 * @param message The message to be logged
 * @param exception The exception to be logged
 *///  w  ww .ja va2s.  c om
private static void log(String message, Throwable exception) {

    log(message);
    exception.printStackTrace(System.out);

}

From source file:com.tesora.dve.common.PEStringUtils.java

/**
 * Get the stack trace as a string value
 * /*from  w  ww.ja va 2  s .c  o m*/
 * @param e
 * @return
 */
public static String toString(Throwable e) {
    StringWriter stringWriter = new StringWriter();
    e.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}

From source file:com.googlecode.icegem.cacheutils.common.Utils.java

public static void exitWithFailure(String message, Throwable t) {
    if (message != null) {
        System.err.println(message);
    }/*  www.jav  a  2 s .  com*/

    if (t != null) {
        t.printStackTrace(System.err);
    }

    System.exit(1);
}

From source file:com.code.savemarks.utils.Utils.java

public static String stackTraceToString(Throwable e) {
    String retValue = null;//from w  ww . j  a  va 2s  .  c o  m
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        retValue = sw.toString();
    } finally {
        try {
            if (pw != null)
                pw.close();
            if (sw != null)
                sw.close();
        } catch (IOException ignore) {
        }
    }
    return retValue;
}

From source file:jp.terasoluna.fw.util.ExceptionUtil.java

/**
 * ?????//from www . j ava2s . c o  m
 * 
 * <p>
 *  ??????????????
 *  ????????
 *  ???getRootCause()????????ServletException??
 * </p>
 *
 * @param throwable 
 * @return ???
 */
public static String getStackTrace(Throwable throwable) {
    StringBuilder sb = new StringBuilder();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while (throwable != null) {
        baos.reset();
        throwable.printStackTrace(new PrintStream(baos));
        sb.append(baos.toString());

        //throwable?Class??
        Class<? extends Throwable> throwableClass = throwable.getClass();

        // ServletException ?? getRootCause ?
        if (SERVLET_EXCEPTION_NAME.equals(throwableClass.getName())) {
            try {
                //throwable = ((ServletException) throwable).getRootCause()
                //Class??????
                Method method = throwableClass.getMethod(GET_ROOT_CAUSE);
                throwable = (Throwable) method.invoke(throwable);
            } catch (NoSuchMethodException e) {
                //???????
                log.error(e.getMessage());
                throwable = null;
            } catch (IllegalAccessException e) {
                //?????????
                log.error(e.getMessage());
                throwable = null;
            } catch (InvocationTargetException e) {
                //?????
                log.error(e.getMessage());
                throwable = null;
            }

        } else {
            throwable = throwable.getCause();
        }
    }
    return sb.toString();
}

From source file:com.zenoss.zenpacks.zenjmx.call.Utility.java

public static void debugStack(Throwable e) {
    if (!_logger.isDebugEnabled())
        return;//from   w  w  w  . j a  v  a2  s .co  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    e.printStackTrace(ps);
    ps.flush();
    String stackTrace = baos.toString();
    _logger.debug(e.getMessage() + "\n" + stackTrace);
}

From source file:com.github.rnewson.couchdb.lucene.Utils.java

public static String error(final int code, final Throwable t) {
    final StringWriter writer = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(writer);
    if (t.getMessage() != null) {
        printWriter.append(t.getMessage());
        printWriter.append("\n");
    }//from  w  w  w . j a v  a2  s. co  m
    t.printStackTrace(printWriter);
    return new JSONObject().element("code", code).element("body", "<pre>" + writer + "</pre>").toString();
}

From source file:es.tunelator.log.Logger.java

/**
 * @param e/* w  w  w. jav  a2  s  .c o m*/
 * @return
 */
private static String getStringStackTrace(Throwable e) {
    StringWriter sw = new StringWriter();
    PrintWriter w = new PrintWriter(sw);
    e.printStackTrace(w);
    return sw.toString();
}