Converts exception stack trace as string - Java java.lang

Java examples for java.lang:Exception StackTrace

Description

Converts exception stack trace as string

Demo Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*from   ww  w.  j av  a 2 s  .c om*/
     * Converts exception stack trace as string
     *
     * @param ex
     * @return
     */
    public static String toStackStrace(Throwable ex) {
        try (StringWriter sw = new StringWriter()) {
            ex.printStackTrace(new PrintWriter(sw));
            return sw.toString();
        } catch (IOException e) {
            return e.getMessage();
        }
    }
}

Related Tutorials