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:org.pgptool.gui.tools.ConsoleExceptionUtils.java

public static String getAllMessages(Throwable t) {
    if (t == null)
        return "";

    StringBuffer ret = new StringBuffer();

    Throwable cur = t;
    while (cur != null) {
        if (cur == cur.getCause())
            break;

        if (ret.length() > 0) {
            ret.append(" -> ");
        }//from w  w  w.  j  a  v a  2s .  com

        if (cur instanceof FieldValidationException) {
            FieldValidationException fve = (FieldValidationException) cur;
            ret.append(buildMessageForFve(fve, LocaleContextHolder.getLocale()));
        } else if (cur instanceof HasMessageCode) {
            ret.append(I18nUtils.buildMessage((HasMessageCode) cur, ac()));
        } else {
            try {
                String className = cur.getClass().getName();
                String messageMappingForClassName = Messages.get(className, cur.getMessage());
                if (className.equals(messageMappingForClassName)) {
                    throw new NoSuchMessageException(className);
                }
                ret.append(messageMappingForClassName);
            } catch (NoSuchMessageException nfe) {
                ret.append(cur.getLocalizedMessage());
            }
        }

        cur = cur.getCause();
    }

    return ret.toString();
}

From source file:Main.java

public static String causeTrace(final Throwable t, final String sep) {
    final StringBuilder sb = new StringBuilder();
    boolean first = true;
    Throwable c = t;
    while (c != null) {
        if (!first)
            sb.append(sep).append("caused by: ");
        sb.append(String.valueOf(c));
        c = c.getCause();
        first = false;//from w  ww. j av  a2 s  .co  m
    }
    return sb.toString();
}

From source file:com.yahoo.elide.core.exceptions.HttpStatusException.java

protected static String formatExceptionCause(Throwable e) {
    // if the throwable has a cause use that, otherwise the throwable is the cause
    Throwable error = e.getCause() == null ? e : e.getCause();
    return error == null ? null : error.getMessage() == null ? error.toString() : error.getMessage();
}

From source file:com.amazonaws.util.Throwables.java

/**
 * Returns the root cause of the given throwable, or null if the given
 * throwable is null. If the root cause is over 1000 level deep, the
 * original throwable will be returned defensively as this is heuristically
 * considered a circular reference, however unlikely.
 *//*from  w  w w .ja  v a2s.  c  o  m*/
public static Throwable getRootCause(Throwable orig) {
    if (orig == null)
        return orig;
    Throwable t = orig;
    // defend against (malicious?) circularity
    for (int i = 0; i < 1000; i++) {
        Throwable cause = t.getCause();
        if (cause == null)
            return t;
        t = cause;
    }
    // Too bad.  Return the original exception.
    LogFactory.getLog(Throwables.class)
            .debug("Possible circular reference detected on " + orig.getClass() + ": [" + orig + "]");
    return orig;
}

From source file:Main.java

/**
 * Copied from "android.util.Log.getStackTraceString()" in order to avoid usage of Android stack
 * in unit tests./*from w ww.  j a v  a  2  s . c o  m*/
 *
 * @return Stack trace in form of String
 */
static String getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }

    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    pw.flush();
    return sw.toString();
}

From source file:net.bpelunit.framework.control.util.BPELUnitUtil.java

/**
 * XPath exception causes tend to be deeply nested. The first meaningful exception (with an
 * error message) is some way down. Find it.
 * /*from   w  w w.  j a va 2 s  .  c om*/
 * @param e
 * @return
 */
public static Throwable findRootThrowable(Exception e) {
    Throwable f = e;
    while (f.getMessage() == null && f.getCause() != null) {
        f = f.getCause();
    }
    return f;
}

From source file:adalid.commons.util.ThrowableUtils.java

public static String getString(Throwable throwable) {
    if (throwable == null) {
        return Throwable.class.getName();
    }/*from w  w  w  .  j  a  va 2 s  .c o  m*/
    String string;
    Throwable cause = throwable.getCause();
    if (cause != null) {
        return getString(cause);
    }
    string = throwable.getLocalizedMessage();
    if (StringUtils.isNotBlank(string)) {
        return getString(string);
    }
    string = throwable.getMessage();
    if (StringUtils.isNotBlank(string)) {
        return getString(string);
    }
    string = throwable.toString();
    if (StringUtils.isNotBlank(string)) {
        return getString(string);
    }
    return Throwable.class.getSimpleName();
}

From source file:com.stgmastek.core.comm.main.StartCoreCommunication.java

static void stop() throws Exception {
    CoreCommServices bServices = ClientBook.getCoreBook().getCoreClientClass(CommConstants.INSTALLATION_CODE);
    try {/* ww  w  .j av  a 2s  .  com*/
        if (logger.isInfoEnabled()) {
            logger.info("Stopping CoreCommServices");
        }
        bServices.stopCoreComm();
    } catch (Throwable e) {
        if (e.getCause() instanceof java.net.ConnectException) {
            if (logger.isEnabledFor(LogLevel.NOTICE)) {
                logger.log(LogLevel.NOTICE,
                        "Stop message transmitted successfully. Please check the core-comm log for successfull termination.");
            }
        }
    }

}

From source file:com.google.gerrit.pgm.util.SiteProgram.java

@SuppressWarnings("deprecation")
private static boolean isCannotCreatePoolException(Throwable why) {
    return why instanceof org.apache.commons.dbcp.SQLNestedException && why.getCause() != null
            && why.getMessage().startsWith("Cannot create PoolableConnectionFactory");
}

From source file:com.kappaware.logtrawler.Utils.java

public static String toString(Throwable e) {
    StringBuffer sb = new StringBuffer();
    sb.append(e.toString());/*w  w  w .j  a  v  a 2 s. co m*/
    while ((e = e.getCause()) != null) {
        sb.append("\nCaused by:");
        sb.append(e.toString());
    }
    ;
    return sb.toString();
}