Java Throwable to String getStackTrace(Throwable aThrowable)

Here you can find the source of getStackTrace(Throwable aThrowable)

Description

getStackTrace:(Describe the usage of this method).

License

Open Source License

Parameter

Parameter Description
aThrowable Throwable exception.

Return

String String of exception stack trace.

Declaration

public static String getStackTrace(Throwable aThrowable) 

Method Source Code


//package com.java2s;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

public class Main {
    /**/*  ww  w.ja v a2 s .  c  om*/
     * getStackTrace:(Describe the usage of this method).  
     * Get string of exception stack trace. 
     * 
     * @param aThrowable
     *          Throwable exception.
     * @return String
     *          String of exception stack trace.
     */
    public static String getStackTrace(Throwable aThrowable) {
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);
        aThrowable.printStackTrace(printWriter);
        return result.toString();
    }

    /**
     * getStackTrace:(Describe the usage of this method).  
     * Get string of exception stack trace. 
     * 
     * @author haoli 
     * @param aThrowable
     *          Throwable exception.
     * @param length
     *          Length limit of the stack trace.
     * @return 
     *          String of exception stack trace.
     */
    public static String getStackTrace(Throwable aThrowable, int length) {
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);
        aThrowable.printStackTrace(printWriter);
        if (result.toString().length() <= length) {
            return result.toString();
        } else {
            return result.toString().substring(0, length - 1);
        }
    }
}

Related

  1. getStackTrace(final Throwable t)
  2. getStackTrace(final Throwable t)
  3. getStackTrace(final Throwable t)
  4. getStackTrace(final Throwable throwable)
  5. getStackTrace(Throwable aThrowable)
  6. getStackTrace(Throwable e)
  7. getStackTrace(Throwable e)
  8. getStackTrace(Throwable e)
  9. getStackTrace(Throwable ex)