Java Exception to String exceptionToString(Throwable e)

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

Description

exception To String

License

Open Source License

Declaration

public static String exceptionToString(Throwable e) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  j  a  v a  2s  .c  o  m
 * ZAL - The abstraction layer for Zimbra.
 * Copyright (C) 2014 ZeXtras S.r.l.
 *
 * This file is part of ZAL.
 *
 * This program 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, version 2 of
 * the License.
 *
 * This program 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 ZAL. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String exceptionToString(Throwable e) {
        StringBuilder sb = new StringBuilder(128);
        StackTraceElement elements[] = e.getStackTrace();

        sb.append(e.toString());
        sb.append("\n");

        for (int n = 0; n < elements.length; ++n) {
            sb.append("        at ");
            sb.append(elements[n].getClassName());
            sb.append(".");
            sb.append(elements[n].getMethodName());
            sb.append(" ( ");
            sb.append(elements[n].getFileName());
            sb.append(":");
            sb.append(elements[n].getLineNumber());
            sb.append(" )");

            if (elements[n].isNativeMethod()) {
                sb.append(" [native]");
            }

            sb.append("\n");
        }

        Throwable cause = e.getCause();
        if (cause != null) {
            sb.append("Caused by: ");
            sb.append(exceptionToString(cause));
        }

        return sb.toString();
    }
}

Related

  1. exceptionStackToString(Throwable x)
  2. exceptionToString(final Exception e)
  3. exceptionToString(final Throwable ex)
  4. exceptionToString(final Throwable throwable)
  5. exceptionToString(Throwable e)
  6. exceptionToString(Throwable ex)
  7. exceptionToString(Throwable exception)
  8. exceptionTypeAndMsg(Exception e)
  9. exceptionWithCause(T exception, Throwable cause)