Java Dump Throwable dumpThrowable(Throwable e)

Here you can find the source of dumpThrowable(Throwable e)

Description

Extracts the stack trace from the throwable to string.

License

Open Source License

Parameter

Parameter Description
e the throwable.

Return

the stack trace in text format.

Declaration

public static String dumpThrowable(Throwable e) 

Method Source Code

//package com.java2s;
/*/*from w w  w.  j av a2 s .c  o  m*/
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

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

public class Main {
    /**
     * Extracts the stack trace from the throwable to string. If the throwable
     * has a cause, the cause will be dumped instead.
     * 
     * @param e the throwable.
     * @return the stack trace in text format.
     */
    public static String dumpThrowable(Throwable e) {
        if (e == null) {
            return null;
        }
        if (e.getCause() != null) {
            e = e.getCause();
        }
        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        return writer.toString();
    }
}

Related

  1. dump(final Throwable throwable)
  2. dumpThrowable(final String additionalDescr, final Throwable t)
  3. dumpThrowable(Throwable thr)