Example usage for java.lang Throwable getCause

List of usage examples for java.lang Throwable getCause

Introduction

In this page you can find the example usage for java.lang Throwable getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.github.dozermapper.core.util.MappingUtils.java

public static Throwable getRootCause(Throwable ex) {
    Throwable rootCause = ex;
    while (rootCause.getCause() != null) {
        rootCause = rootCause.getCause();
    }/*from   w  ww  .ja v a2 s  . c  om*/
    return rootCause;
}

From source file:com.notonthehighstreet.ratel.internal.model.Error.java

private static List<Backtrace> unwrapStackTrace(final Throwable e) {
    // As Honeybadger only supports a single error, we need to massage the wrapped exceptions into a single stack trace
    final List<Backtrace> backtraces = new ArrayList<Backtrace>();

    Throwable cause = e;

    while (cause != null) {
        if (!backtraces.isEmpty()) {
            backtraces.add(Backtrace.markerBacktrace(cause));
        }/*  ww w. j  a v a 2s. com*/

        for (final StackTraceElement element : e.getStackTrace()) {
            backtraces.add(Backtrace.fromStackTrace(element));
        }

        cause = cause.getCause();
    }

    return backtraces;
}

From source file:com.amazonaws.eclipse.core.diagnostic.utils.AwsPortalFeedbackFormUtils.java

public static String getStackTraceFromThrowable(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    t.printStackTrace(pw);// w w w.  ja  v  a 2s . co m
    pw.println();

    if (t.getCause() != null) {
        t.getCause().printStackTrace(pw);
    }

    return sw.toString();
}

From source file:de.huxhorn.sulky.io.IOUtilities.java

/**
 * This method calls Thread.currentThread().interrupt() if any exception
 * in the hierarchy (including all getCause()) is either InterruptedIOException
 * or InterruptedException./*  ww  w  .j  a  v a  2s .c  o m*/
 *
 * This method should be called in every catch(IOException), catch(Exception) or
 * catch(Throwable) block.
 *
 * @param t the Throwable to be checked for interruption. Does nothing if null.
 */
public static void interruptIfNecessary(Throwable t) {
    if (t == null) {
        return;
    }
    Throwable current = t;
    do {
        if (current instanceof InterruptedIOException || current instanceof InterruptedException) {
            Thread.currentThread().interrupt();
            break;
        }
        current = current.getCause();
    } while (current != null);
}

From source file:com.aw.support.reflection.MethodInvoker.java

public static Object invoke(Object target, String methodName, Object[] parameters, Class[] parameterTypes) {
    Object result = null;/*from   ww  w.  ja  va2  s.  c o m*/
    try {
        Class cls = target.getClass();
        Class[] paramTypes = parameterTypes;
        for (int i = 0; i < paramTypes.length; i++) {
            if (parameterTypes[i] == null) {
                paramTypes[i] = parameters[i].getClass();
            }
        }
        Method method = cls.getMethod(methodName, paramTypes);
        method.setAccessible(true);
        result = method.invoke(target, parameters);
    } catch (Throwable e) {
        if (!(e instanceof AWException)) {
            if (e.getCause() instanceof AWException) {
                throw ((AWException) e.getCause());
            }
            e.printStackTrace();
            throw new AWSystemException("Problems calling the method:" + methodName, e);
        } else {
            throw (AWException) e;
        }
    }
    return result;
}

From source file:com.runwaysdk.controller.ErrorUtility.java

private static Throwable filterServletException(Throwable t) {
    int i = 0;/*from   w w  w  .ja  v a  2s . c o  m*/
    while (t instanceof ServletException && i < 50) {
        t = t.getCause();
        i++;
    }
    return t;
}

From source file:org.commonjava.aprox.bind.jaxrs.util.ResponseUtils.java

public static CharSequence formatEntity(final Throwable error, final String message) {
    final StringWriter sw = new StringWriter();
    if (message != null) {
        sw.append(message);//from  w  ww . java  2 s .  com
        sw.append("\nError was:\n\n");
    }

    sw.append(error.getMessage());

    final Throwable cause = error.getCause();
    if (cause != null) {
        sw.append("\n\n");
        cause.printStackTrace(new PrintWriter(sw));
    }

    sw.write('\n');

    return sw.toString();
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void alternatePrintSQLException(SQLException ex) {
    while (ex != null) {
        System.err.println("SQLState: " + ex.getSQLState());
        System.err.println("Error Code: " + ex.getErrorCode());
        System.err.println("Message: " + ex.getMessage());
        Throwable t = ex.getCause();
        while (t != null) {
            System.out.println("Cause: " + t);
            t = t.getCause();
        }//from  w  ww.  ja v a  2 s.  c  o m
        ex = ex.getNextException();
    }
}

From source file:com.seleniumtests.reporter.reporters.CommonReporter.java

/**
 * Method to generate the formated stacktrace
 * @param exception      Exception to format
 * @param title         title of the exception
 * @param contentBuffer   //from ww w .  j  a v  a  2 s .co m
 * @param format      format to use to encode ('html', 'csv', 'xml', 'json')
 */
public static void generateTheStackTrace(final Throwable exception, final String title,
        final StringBuilder contentBuffer, String format) {
    contentBuffer.append(exception.getClass() + ": " + StringUtility.encodeString(title, format) + "\n");

    StackTraceElement[] s1 = exception.getStackTrace();
    Throwable t2 = exception.getCause();
    if (t2 == exception) {
        t2 = null;
    }
    for (int x = 0; x < s1.length; x++) {
        String message = filterStackTrace(s1[x].toString());
        if (message != null) {
            contentBuffer.append("\nat " + StringUtility.encodeString(message, format));
        }
    }

    if (t2 != null) {
        generateTheStackTrace(t2, "Caused by " + t2.getLocalizedMessage(), contentBuffer, format);
    }
}

From source file:cc.sion.core.utils.Exceptions.java

/**
 * StackTrace. ?StackTrace?, ????./*from  w w w  .j  av  a2  s.  c  o m*/
 *
 * ?StackTrace???(logger)?Trace.
 *
 * Cause??, ??CauseStackTrace.
 */
public static <T extends Throwable> T clearStackTrace(T exception) {
    Throwable cause = exception;
    while (cause != null) {
        cause.setStackTrace(EMPTY_STACK_TRACE);
        cause = cause.getCause();
    }
    return exception;// NOSONAR
}