Java Exception to String exceptionToString(Throwable ex)

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

Description

exception To String

License

LGPL

Declaration

public static String exceptionToString(Throwable ex) 

Method Source Code

//package com.java2s;
/*//from ww w  . j  a  v a  2s .co m
 * ============================================================================
 * GNU Lesser General Public License
 * ============================================================================
 *
 * ZKTest - Free ZeroKode testing library.
 * Copyright (C) 2011 Telesoft Consulting GmbH http://www.telesoft-consulting.at
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; If not, see http://www.gnu.org/licenses/
 * 
 * Telesoft Consulting GmbH
 * Gumpendorferstra?e 83-85
 * House 1, 1st Floor, Office No.1
 * 1060 Vienna, Austria
 * http://www.telesoft-consulting.at/
 */

public class Main {
    public static String exceptionToString(Throwable ex) {
        return exceptionToString(ex, "");
    }

    public static String exceptionToString(Throwable ex, String tabs) {
        StringBuilder sb = new StringBuilder();
        try {
            sb.append(tabs).append("Exception      : ").append(ex.getClass().getSimpleName()).append("\n");
            sb.append(tabs).append("Thread         : ").append(Thread.currentThread().getName()).append("\n");
            sb.append(tabs).append("Message        : ").append(ex.getMessage()).append("\n");

            StackTraceElement[] stack = ex.getStackTrace();
            sb.append(tabs).append("Stacktrace     : \n").append(stackTraceToString(stack, tabs));
            Throwable cause = ex.getCause();

            if (cause != null && !cause.equals(ex)) {
                sb.append(tabs).append("InnerException : \n").append(exceptionToString(cause, tabs + "    "));
            }
        } catch (Throwable exConv) {
        }
        return sb.toString();
    }

    public static String stackTraceToString(StackTraceElement[] stack, String tabs) {
        String ret = "";
        for (int i = 0; i < stack.length; i++) {
            StackTraceElement elem = stack[i];
            ret += tabs + "    " + elem.toString() + "\n";
        }
        return ret;
    }
}

Related

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