Java Stacktrace Print formatStackTraceToString(Exception ex)

Here you can find the source of formatStackTraceToString(Exception ex)

Description

format Stack Trace To String

License

Open Source License

Declaration

public static String formatStackTraceToString(Exception ex) 

Method Source Code


//package com.java2s;
/*/* ww  w.  j a va  2 s .c  o  m*/
 * Copyright (C) 2014 Vitor Hugo Salgado <vsalgadopb@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    public static String formatStackTraceToString(Exception ex) {

        if (ex == null) {
            throw new IllegalArgumentException("ex can't be null.");
        }

        String result = null;
        StringWriter stringWriter = null;
        PrintWriter printWriter = null;

        try {
            stringWriter = new StringWriter();
            printWriter = new PrintWriter(stringWriter);
            ex.printStackTrace(printWriter);
            result = stringWriter.toString();
        } finally {
            try {
                if (stringWriter != null) {
                    stringWriter.close();
                }
                if (printWriter != null) {
                    printWriter.close();
                }
            } catch (IOException e) {
                //
            }
        }

        return result;
    }
}

Related

  1. formatStackTrace(Thread thread)
  2. formatStackTrace(Throwable t)
  3. formatStackTrace(Throwable t)
  4. formatStackTrace(Throwable t, String prefix)
  5. printStackTrace()
  6. printStackTrace()
  7. printStackTrace()
  8. printStackTrace()