Java Throwable to String getStackTrace(final Throwable exception)

Here you can find the source of getStackTrace(final Throwable exception)

Description

Returns the exception trace in the form of string.

License

Open Source License

Parameter

Parameter Description
exception The exception to convert as a string.

Return

A non-null string but possibly empty.

Declaration

public static String getStackTrace(final Throwable exception) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation.                                         *
 * All rights reserved. This program and the accompanying materials            *
 * are made available under the terms of the Eclipse Public License v1.0       *
 * which accompanies this distribution, and is available at                    *
 * http://www.eclipse.org/legal/epl-v10.html                                   *
 *******************************************************************************/

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

public class Main {
    /**/*from w ww  .  java 2s  .c  o  m*/
     * Returns the exception trace in the form of string. If the exception transmitted as a cause object exception non-null, 
     * this will be taken as the source for the string extraction.
     * 
     * @param exception The exception to convert as a string.
     * @return A non-null string but possibly empty.
     */
    public static String getStackTrace(final Throwable exception) {
        final StringWriter strWriter = new StringWriter();
        if (exception.getCause() == null) {
            exception.printStackTrace(new PrintWriter(strWriter, true));
        } else {
            exception.getCause().printStackTrace(new PrintWriter(strWriter, true));
        }
        strWriter.flush();
        return strWriter.toString();
    }
}

Related

  1. getStackTrace (final Throwable e)
  2. getStackTrace(@Nonnull final Throwable throwable)
  3. getStackTrace(final Throwable aThrowable)
  4. getStackTrace(final Throwable e)
  5. getStackTrace(final Throwable error)
  6. getStackTrace(final Throwable t)
  7. getStackTrace(final Throwable t)
  8. getStackTrace(final Throwable t)
  9. getStackTrace(final Throwable t)