Function used to get the error trace from the exception object - Java java.lang

Java examples for java.lang:Exception

Description

Function used to get the error trace from the exception object

Demo Code


//package com.java2s;

public class Main {
    /**//from   www.  j  ava2  s  .  c om
     * Function used to get the error trace from the exception object
     * @param e
     * @return
     * @throws Exception
     */
    public static String stackTraceToString(Throwable e) {
        StringBuilder sb = new StringBuilder();
        try {
            for (StackTraceElement element : e.getStackTrace()) {
                sb.append(element.toString());
                sb.append("\n");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return sb.toString();
    }
}

Related Tutorials