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:Main.java

public static String toString(Throwable error) {
    StringBuffer buf = new StringBuffer(512);
    while (error != null) {
        buf.append(error.getClass().getName());
        buf.append(error.getMessage() == null ? "" : error.getMessage());
        buf.append("\r\n");
        StackTraceElement[] stack = error.getStackTrace();
        for (int i = 0; i < stack.length; i++) {
            buf.append("   ");
            buf.append(stack[i].toString());
            buf.append("\r\n");
        }// www .  ja v a  2  s.c o  m
        if (error.getCause() != null) {
            buf.append("\r\nCaused by :");
        }
        error = error.getCause();
    }
    return buf.toString();
}

From source file:eu.delving.services.core.Harvindexer.java

private static String exceptionToErrorString(HarvindexingException exception) {
    StringBuilder out = new StringBuilder();
    out.append(exception.getMessage());//from   w w  w.  ja  v a  2  s  .c  o m
    Throwable cause = exception.getCause();
    while (cause != null) {
        out.append('\n');
        out.append(cause.toString());
        cause = cause.getCause();
    }
    return out.toString();
}

From source file:me.j360.dubbo.modules.util.base.ExceptionUtil.java

/**
 * ??./*from  w w  w .j a  va  2 s  . c o m*/
 */
@SuppressWarnings("unchecked")
public static boolean isCausedBy(Throwable t, Class<? extends Exception>... causeExceptionClasses) {
    Throwable cause = t;

    while (cause != null) {
        for (Class<? extends Exception> causeClass : causeExceptionClasses) {
            if (causeClass.isInstance(cause)) {
                return true;
            }
        }
        cause = cause.getCause();
    }
    return false;
}

From source file:org.commonjava.cartographer.rest.util.ResponseUtils.java

public static CharSequence formatEntity(final String id, final Throwable error, final String message) {
    final StringWriter sw = new StringWriter();
    sw.append("Id: ").append(id);
    if (message != null) {
        sw.append("\nMessage: ").append(message);
    }//from   w w  w . jav a  2  s  . c o  m

    sw.append(error.getMessage());

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

    sw.write('\n');

    return sw.toString();
}

From source file:fr.xebia.audit.AuditAspect.java

protected static void appendThrowableCauses(Throwable throwable, String separator, StringBuilder toAppendTo) {
    List<Throwable> alreadyAppendedThrowables = new ArrayList<Throwable>();

    while (throwable != null) {
        // append
        toAppendTo.append(throwable.toString());
        alreadyAppendedThrowables.add(throwable);

        // cause// www.  ja  v a  2 s . c  o m
        Throwable cause = throwable.getCause();
        if (cause == null || alreadyAppendedThrowables.contains(cause)) {
            break;
        } else {
            throwable = cause;
            toAppendTo.append(separator);
        }
    }
}

From source file:Util.java

/**
 * Looks up and returns the root cause of an exception. If none is found, returns
 * supplied Throwable object unchanged. If root is found, recursively "unwraps" it,
 * and returns the result to the user./*from  w  w w  . j av  a  2  s  . c o  m*/
 */
public static Throwable unwindException(Throwable th) {
    if (th instanceof SAXException) {
        SAXException sax = (SAXException) th;
        if (sax.getException() != null) {
            return unwindException(sax.getException());
        }
    } else if (th instanceof SQLException) {
        SQLException sql = (SQLException) th;
        if (sql.getNextException() != null) {
            return unwindException(sql.getNextException());
        }
    } else if (th.getCause() != null) {
        return unwindException(th.getCause());
    }

    return th;
}

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

/**
 * ??./*from   w w w  .  ja va 2 s.  c o  m*/
 */
public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) {
    Throwable cause = ex;
    while (cause != null) {
        for (Class<? extends Exception> causeClass : causeExceptionClasses) {
            if (causeClass.isInstance(cause)) {
                return true;
            }
        }
        cause = cause.getCause();
    }
    return false;
}

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

/**
 * ??./*w ww .j  a  va2s . c  om*/
 */
public static boolean isCausedBy(Throwable t, Class<? extends Exception>... causeExceptionClasses) {
    Throwable cause = t;

    while (cause != null) {
        for (Class<? extends Exception> causeClass : causeExceptionClasses) {
            if (causeClass.isInstance(cause)) {
                return true;
            }
        }
        cause = cause.getCause();
    }
    return false;
}

From source file:controllers.Common.java

@Util
public static Throwable unwrap(Throwable t) {
    Throwable cause = t;
    while ((cause instanceof UnexpectedException) || (cause instanceof ExecutionException)) {
        cause = cause.getCause();
    }/*from ww  w  . j  a  v  a2  s . c  o m*/
    return cause != null ? cause : t;
}

From source file:org.eclipse.cft.server.core.internal.CloudErrorUtil.java

public static CoreException checkRestException(Throwable t) {
    String error = getInvalidCredentialsError(t);
    if (error == null) {

        if (t instanceof ResourceAccessException && t.getCause() instanceof UnknownHostException) {
            error = Messages.ERROR_UNABLE_TO_ESTABLISH_CONNECTION_UNKNOWN_HOST;
        } else if (t instanceof RestClientException) {
            error = NLS.bind(Messages.ERROR_FAILED_REST_CLIENT, t.getMessage());
        }//from w ww  .ja  v a2 s.  c  o m

    }
    return error != null ? toCoreException(error, t) : toCoreException(t);
}