Java Stacktrace to String getStackTrace(Exception e)

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

Description

get Stack Trace

License

Apache License

Declaration

public static String getStackTrace(Exception e) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;

import java.io.PrintStream;
import java.io.StringReader;

public class Main {
    public static String getStackTrace(Exception e) {
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArray, true));
        BufferedReader reader = new BufferedReader(new StringReader(byteArray.toString()));
        String line;//from w w w .j  a  v  a 2  s.  com
        int count = 0;
        String result = "";
        try {
            while (count++ < 6 && (line = reader.readLine()) != null) {
                result += line + "\n";
            }
        } catch (Exception ee) {
            // should never happen
            ee.printStackTrace();
        }
        return result;
    }
}

Related

  1. getStackTrace()
  2. getStackTrace()
  3. getStackTrace(Exception e)
  4. getStackTrace(Exception e)
  5. getStackTrace(Exception e)
  6. getStackTrace(Exception exception)