Example usage for java.lang Throwable getClass

List of usage examples for java.lang Throwable getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:net.sf.taverna.t2.activities.beanshell.BeanshellActivity.java

private static String determineMessage(Throwable e) {
    if (e instanceof TargetError) {
        Throwable t = ((TargetError) e).getTarget();
        if (t != null) {
            return t.getClass().getCanonicalName() + ": " + determineMessage(t);
        }/*from ww  w .  j  ava  2  s .  co m*/
    }
    Throwable cause = e.getCause();
    if (cause != null) {
        return determineMessage(cause);
    }
    return e.getMessage();
}

From source file:de.micromata.genome.util.runtime.ExceptionUtils.java

/**
 * Unwrapp an rethrow Throwable as Exception or Error.
 *
 * @param ex the ex/*from  ww  w  .ja v  a2  s . c o m*/
 * @throws Exception the exception
 */
public static void throwUnwrappedThrowableToException(Throwable ex) throws Exception {
    if (ex instanceof Error) {
        throw (Error) ex;
    }
    if (ex instanceof Exception) {
        throw (Exception) ex;
    }
    throw new IllegalArgumentException("Exception is Neither error or Exception: " + ex.getClass(), ex);
}

From source file:com.atlassian.jira.rest.client.TestUtil.java

@SuppressWarnings("unused")
public static <T extends Throwable> void assertThrows(Class<T> clazz, String regexp, Runnable runnable) {
    try {/*from   w  w w  .  j  a v  a2s .  co  m*/
        runnable.run();
        Assert.fail(clazz.getName() + " exception expected");
    } catch (Throwable e) {
        Assert.assertTrue(
                "Expected exception of class " + clazz.getName() + " but was caught " + e.getClass().getName(),
                clazz.isInstance(e));
        if (e.getMessage() == null && regexp != null) {
            Assert.fail("Exception with no message caught, while expected regexp [" + regexp + "]");
        }
        if (regexp != null && e.getMessage() != null) {
            Assert.assertTrue("Message [" + e.getMessage() + "] does not match regexp [" + regexp + "]",
                    e.getMessage().matches(regexp));
        }
    }

}

From source file:com.android.tools.idea.diagnostics.crash.CrashReport.java

/**
 * Returns an exception description (similar to {@link ExceptionUtil#getThrowableText(Throwable)} with the exception message
 * removed in order to strip off any PII. The exception message is include for some specific exceptions where we know that the
 * message will not have any PII./*  ww  w  . j  av  a2  s  .  c  o  m*/
 */
@NotNull
public static String getDescription(@NotNull Throwable t) {
    if (THROWABLE_CLASSES_TO_TRACK_MESSAGES.contains(t.getClass())) {
        return ExceptionUtil.getThrowableText(t);
    }

    StringBuilder sb = new StringBuilder(256);

    sb.append(t.getClass().getName());
    sb.append(": <elided>\n"); // note: some message is needed for the backend to parse the report properly

    for (StackTraceElement el : t.getStackTrace()) {
        sb.append("\tat ");
        sb.append(el);
        sb.append('\n');
    }

    return sb.toString();
}

From source file:com.crossbusiness.resiliency.aspect.AbstractFallbackAspect.java

static boolean isFallbackableException(Throwable t, Class<? extends Throwable>[] fallbackableExceptions) {
    for (Class<? extends Throwable> throwable : fallbackableExceptions) {
        if (throwable.isAssignableFrom(t.getClass())) {
            return true;
        }/*from   w ww .j a  v  a 2  s .com*/
    }
    return false;
}

From source file:org.cleverbus.core.common.exception.ExceptionTranslator.java

/**
 * Returns recursively error messages from the whole exception hierarchy.
 *
 * @param ex the exception//from   w w w .  j  a va 2 s  . c  o  m
 * @return exception message
 */
private static String getMessagesInExceptionHierarchy(Throwable ex) {
    Assert.notNull(ex, "the ex must not be null");

    // use a util that can handle recursive cause structure:
    Throwable[] hierarchy = ExceptionUtils.getThrowables(ex);

    StringBuilder messages = new StringBuilder();
    for (Throwable throwable : hierarchy) {
        if (messages.length() > 0) {
            messages.append(" => ");
        }
        messages.append(throwable.getClass().getSimpleName()).append(": ").append(throwable.getMessage());
    }

    return messages.toString();
}

From source file:org.echocat.jemoni.jmx.support.SpringUtils.java

@Nonnull
public static <T> T getBeanFor(@Nonnull ServletContext servletContext, @Nonnull String beanName,
        @Nonnull Class<T> beanType) throws ServletException {
    final Object applicationContext = getApplicationContext(servletContext);
    final Object plainBean;
    try {/*from   ww  w  .java  2  s  . c o  m*/
        plainBean = beanType.cast(GET_BEAN.invoke(applicationContext, beanName));
    } catch (Exception e) {
        final Throwable target = e instanceof InvocationTargetException
                ? ((InvocationTargetException) e).getTargetException()
                : null;
        if (target != null && target.getClass().getName().endsWith("NoSuchBeanDefinitionException")) {
            throw new ServletException("Could not find bean '" + beanName + "' at " + applicationContext + ".",
                    target);
        } else {
            throw new ServletException(
                    "Could not retrieve bean '" + beanName + "' from " + applicationContext + ".",
                    target != null ? target : e);
        }
    }
    if (!beanType.isInstance(plainBean)) {
        throw new ServletException("Could bean '" + beanName + "' is of type " + plainBean.getClass().getName()
                + " not of expected " + beanType.getName() + ".");
    }
    return beanType.cast(plainBean);
}

From source file:eu.stratosphere.pact.runtime.sort.AsynchonousPartialSorterITCase.java

private static boolean containsTriggerException(Throwable exception) {
    while (exception != null) {
        if (exception.getClass().equals(TriggeredException.class)) {
            return true;
        }//w w w. j  a  v  a2s.c o  m
        exception = exception.getCause();
    }
    return false;
}

From source file:Main.java

/**
 * Check if the given <code>java.lang.Throwable</code> is a declared
 * throwable of the given method.//from   w  w w . j a  v  a  2  s . c o  m
 * @param p_e The trowable.
 * @param p_method The method.
 * @return <code>true</code> if <code>p_method</code> declares to throw
 *    <code>p_e</code>, or <code>false</code> otherwise.
 * @throws IllegalArgumentException
 *    If either of the method parameters is <code>null</code>.
 */
public static boolean isDeclaredThrowable(Throwable p_e, Method p_method) {
    final Class<? extends Throwable> l_clsThrowable;

    if (p_e == null)
        throw new IllegalArgumentException("No throwable.");

    if (p_method == null)
        throw new IllegalArgumentException("No method.");

    l_clsThrowable = p_e.getClass();

    for (final Class<?> l_clsException : p_method.getExceptionTypes()) {
        if (l_clsException.isAssignableFrom(l_clsThrowable))
            return true;
    }

    return false;
}

From source file:at.wada811.dayscounter.CrashExceptionHandler.java

/**
 * ?/*from w  w w . j a  v  a2s .  com*/
 *
 * @param throwable
 *
 * @return
 *
 * @throws JSONException
 */
public static JSONObject getExceptionInfo(Throwable throwable) throws JSONException {
    JSONObject json = new JSONObject();
    json.put("name", throwable.getClass().getName());
    json.put("message", throwable.getMessage());
    if (throwable.getStackTrace() != null) {
        // ExceptionStacktrace
        JSONArray exceptionStacktrace = new JSONArray();
        for (StackTraceElement element : throwable.getStackTrace()) {
            exceptionStacktrace.put("at " + LogUtils.getMetaInfo(element));
        }
        json.put("ExceptionStacktrace", exceptionStacktrace);
    }
    if (throwable.getCause() != null) {
        json.put("cause", throwable.getCause());
        // CausedStacktrace
        if (throwable.getCause().getStackTrace() != null) {
            JSONArray causedStacktrace = new JSONArray();
            for (StackTraceElement element : throwable.getCause().getStackTrace()) {
                causedStacktrace.put("at " + LogUtils.getMetaInfo(element));
            }
            json.put("CausedStacktrace", causedStacktrace);
        }
    }
    return json;
}