Java Throwable to String getExceptionPrintout(Throwable aThrowable)

Here you can find the source of getExceptionPrintout(Throwable aThrowable)

Description

Defines a custom format for the stack trace as String.

License

Open Source License

Declaration

public static String getExceptionPrintout(Throwable aThrowable) 

Method Source Code

//package com.java2s;
/**// ww  w.ja va 2s .  c om
 * Copyright 2011 The ARIES Consortium (http://www.ariesonline.org) and
 * www.integratedmodelling.org. 
    
   This file is part of Thinklab.
    
   Thinklab 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.
    
   Thinklab 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 Thinklab.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

public class Main {
    /**
     * Defines a custom format for the stack trace as String.
     */
    public static String getExceptionPrintout(Throwable aThrowable) {

        //add the class name and any message passed to constructor
        final StringBuilder result = new StringBuilder();
        result.append(aThrowable.toString());
        final String NEW_LINE = System.getProperty("line.separator");
        result.append(NEW_LINE);

        //add each element of the stack trace
        for (StackTraceElement element : aThrowable.getStackTrace()) {
            result.append(element);
            result.append(NEW_LINE);
        }
        return result.toString();
    }

    public static String getStackTrace(Throwable aThrowable) {
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);
        aThrowable.printStackTrace(printWriter);
        return result.toString();
    }
}

Related

  1. getExceptionAsString(Throwable _ex)
  2. getExceptionDescription(Throwable e)
  3. getExceptionDetails (final Throwable e)
  4. getExceptionDetails(Throwable throwable)
  5. getExceptionHeadline(Throwable t)
  6. getExceptionStack(final Throwable e)
  7. getExceptionStack(Throwable e)
  8. getExceptionStackTrace(Throwable ex)
  9. getExceptionStackTrace(Throwable exception)