Java Throwable to String stackTrace(Throwable th, String pattern)

Here you can find the source of stackTrace(Throwable th, String pattern)

Description

Return the Stacktrace as String Optional filter every line with Pattern String

License

Open Source License

Parameter

Parameter Description
th a parameter
p a parameter

Declaration

public static String stackTrace(Throwable th, String pattern) 

Method Source Code


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

import java.io.BufferedReader;

import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

public class Main {
    /**/*  ww w.  j av  a2 s .c o  m*/
     * Return the Stacktrace as String
     * Optional filter every line with Pattern String
     * @param th
     * @param p
     * @return
     */
    public static String stackTrace(Throwable th, String pattern) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        th.printStackTrace(pw);
        if (pattern != null) {
            BufferedReader br = new BufferedReader(new StringReader(sw.toString()));
            sw = new StringWriter();
            String s = null;
            try {
                while ((s = br.readLine()) != null) {
                    if (s.matches(pattern))
                        sw.write(s + "\n");
                }
            } catch (Exception e) {

            }
        }
        return sw.toString();
    }
}

Related

  1. stackTrace(Throwable exception)
  2. stackTrace(Throwable t)
  3. stackTrace(Throwable t)
  4. stackTrace(Throwable t)
  5. stackTrace(Throwable th)
  6. stackTrace(Throwable throwable)
  7. stackTraceAsString(final Throwable aError)
  8. stackTraceAsString(Throwable e)
  9. stackTraceAsString(Throwable throwable)