Java Stacktrace to String getStackTraceAsString(String pstrMessage, Exception poException)

Here you can find the source of getStackTraceAsString(String pstrMessage, Exception poException)

Description

Writes out the stack trace into a string and returns it along with the supplied message.

License

Open Source License

Parameter

Parameter Description
pstrMessage custom exception message to prepend to the output
poException a parameter

Return

exception message string with a stack trace

Declaration

public static String getStackTraceAsString(String pstrMessage, Exception poException) 

Method Source Code


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

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class Main {
    /**/*from   w  w  w.  j  a  v a 2  s.c o  m*/
     * Writes out the stack trace into a string and returns it
     * along with the supplied message.
     * @param pstrMessage custom exception message to prepend to the output
     * @param poException
     * @return exception message string with a stack trace
     */
    public static String getStackTraceAsString(String pstrMessage, Exception poException) {
        String strMessage = "";

        // Based on PostgreSQL JDBC driver's PGSQLException.
        try {
            ByteArrayOutputStream oByteArrayOutputStream = new ByteArrayOutputStream();
            PrintWriter oPrintWriter = new PrintWriter(oByteArrayOutputStream);

            oPrintWriter.println("Exception: " + poException + "\nStack Trace:\n");
            poException.printStackTrace(oPrintWriter);
            oPrintWriter.println("End of Stack Trace");

            strMessage += pstrMessage + " " + oByteArrayOutputStream;

            oPrintWriter.println(strMessage);

            oPrintWriter.flush();
            oPrintWriter.close();

            oByteArrayOutputStream.close();
        } catch (Exception e) {
            strMessage += pstrMessage + " " + poException + "\nError on stack trace generation: " + e;
        }

        return strMessage;
    }
}

Related

  1. getStackTrace(Exception x)
  2. getStacktraceAsString(Exception e)
  3. getStacktraceAsString(Exception e)
  4. getStackTraceAsString(Exception e)
  5. getStackTraceAsString(Exception e)
  6. getStackTraceFromException(Exception _e)
  7. getStackTraceFromException(Exception e)
  8. getStackTraces()
  9. getStackTraceStr(Exception e)