java.lang.Throwable.getCause()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(475)

本文整理了Java中java.lang.Throwable.getCause()方法的一些代码示例,展示了Throwable.getCause()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Throwable.getCause()方法的具体详情如下:
包路径:java.lang.Throwable
类名称:Throwable
方法名:getCause

Throwable.getCause介绍

[英]Returns the cause of this Throwable, or null if there is no cause.
[中]返回此可丢弃的原因,如果没有原因,则返回null。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check "javax.servlet.error.exception" attribute for a multipart exception.
 */
private boolean hasMultipartException(HttpServletRequest request) {
  Throwable error = (Throwable) request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE);
  while (error != null) {
    if (error instanceof MultipartException) {
      return true;
    }
    error = error.getCause();
  }
  return false;
}

代码示例来源:origin: ReactiveX/RxJava

private List<Throwable> getListOfCauses(Throwable ex) {
  List<Throwable> list = new ArrayList<Throwable>();
  Throwable root = ex.getCause();
  if (root == null || root == ex) {
    return list;
  } else {
    while (true) {
      list.add(root);
      Throwable cause = root.getCause();
      if (cause == null || cause == root) {
        return list;
      } else {
        root = cause;
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check the source behind this error: possibly an {@link Exception}
 * (typically {@link org.springframework.beans.PropertyAccessException})
 * or a Bean Validation {@link javax.validation.ConstraintViolation}.
 * <p>The cause of the outermost exception will be introspected as well,
 * e.g. the underlying conversion exception or exception thrown from a setter
 * (instead of having to unwrap the {@code PropertyAccessException} in turn).
 * @return whether this error has been caused by a source object of the given type
 * @since 5.0.4
 */
public boolean contains(Class<?> sourceType) {
  return (sourceType.isInstance(this.source) ||
      (this.source instanceof Throwable && sourceType.isInstance(((Throwable) this.source).getCause())));
}

代码示例来源:origin: ReactiveX/RxJava

private void appendStackTrace(StringBuilder b, Throwable ex, String prefix) {
  b.append(prefix).append(ex).append('\n');
  for (StackTraceElement stackElement : ex.getStackTrace()) {
    b.append("\t\tat ").append(stackElement).append('\n');
  }
  if (ex.getCause() != null) {
    b.append("\tCaused by: ");
    appendStackTrace(b, ex.getCause(), "");
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
protected int getErrorCode(Throwable e) {
  if (e instanceof Fault) {
    e = e.getCause();
  }
  if (e instanceof SocketTimeoutException) {
    return RpcException.TIMEOUT_EXCEPTION;
  } else if (e instanceof IOException) {
    return RpcException.NETWORK_EXCEPTION;
  }
  return super.getErrorCode(e);
}

代码示例来源:origin: ReactiveX/RxJava

private static Throwable getRootCause(Throwable ex) {
  Throwable root = ex.getCause();
  if (root == null) {
    return null;
  } else {
    while (true) {
      if (root.getCause() == null) {
        return root;
      } else {
        root = root.getCause();
      }
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
protected int getErrorCode(Throwable e) {
  if (e instanceof Fault) {
    e = e.getCause();
  }
  if (e instanceof SocketTimeoutException) {
    return RpcException.TIMEOUT_EXCEPTION;
  } else if (e instanceof IOException) {
    return RpcException.NETWORK_EXCEPTION;
  }
  return super.getErrorCode(e);
}

代码示例来源:origin: google/guava

/**
  * Appends the chain of messages from the {@code conflictingStackTrace} to the original {@code
  * message}.
  */
 @Override
 public String getMessage() {
  StringBuilder message = new StringBuilder(super.getMessage());
  for (Throwable t = conflictingStackTrace; t != null; t = t.getCause()) {
   message.append(", ").append(t.getMessage());
  }
  return message.toString();
 }
}

代码示例来源:origin: google/guava

private static void rethrow(ExecutionException e) throws ExecutionException {
 Throwable wrapper = e.getCause();
 if (wrapper instanceof WrapperException) {
  Throwable cause = wrapper.getCause();
  if (cause instanceof RuntimeException) {
   throw (RuntimeException) cause;
  } else if (cause instanceof Error) {
   throw (Error) cause;
  }
 }
 throw e;
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private HttpStatus resolveStatus(Throwable ex) {
  HttpStatus status = determineStatus(ex);
  if (status == null) {
    Throwable cause = ex.getCause();
    if (cause != null) {
      status = resolveStatus(cause);
    }
  }
  return status;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(Object item) {
  Throwable cause = null;
  if (item != null && item instanceof Throwable) {
    cause = ((Throwable)item).getCause();
  }
  return matcher.matches(cause);
}

代码示例来源:origin: skylot/jadx

private static void filterRecursive(Throwable th) {
  try {
    filter(th);
  } catch (Exception e) {
    // ignore filter exceptions
  }
  Throwable cause = th.getCause();
  if (cause != null) {
    filterRecursive(cause);
  }
}

代码示例来源:origin: ReactiveX/RxJava

public static void assertUndeliverable(List<Throwable> list, int index, Class<? extends Throwable> clazz) {
  Throwable ex = list.get(index);
  if (!(ex instanceof UndeliverableException)) {
    AssertionError err = new AssertionError("Outer exception UndeliverableException expected but got " + list.get(index));
    err.initCause(list.get(index));
    throw err;
  }
  ex = ex.getCause();
  if (!clazz.isInstance(ex)) {
    AssertionError err = new AssertionError("Inner exception " + clazz + " expected but got " + list.get(index));
    err.initCause(list.get(index));
    throw err;
  }
}

代码示例来源:origin: ReactiveX/RxJava

/**
 * Cast the given Throwable to CompositeException and returns its inner
 * Throwable list.
 * @param ex the target Throwable
 * @return the list of Throwables
 */
public static List<Throwable> compositeList(Throwable ex) {
  if (ex instanceof UndeliverableException) {
    ex = ex.getCause();
  }
  return ((CompositeException)ex).getExceptions();
}

代码示例来源:origin: ReactiveX/RxJava

static void assertUndeliverableTestException(List<Throwable> list, int index, String message) {
  assertTrue(list.get(index).toString(), list.get(index).getCause() instanceof TestException);
  assertEquals(message, list.get(index).getCause().getMessage());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void nullRootCause() {
  RuntimeException te = new RuntimeException() {
    private static final long serialVersionUID = -8492568224555229753L;
    @Override
    public Throwable getCause() {
      return null;
    }
  };
  assertSame(te, new CompositeException(te).getCause().getCause());
  assertSame(te, new CompositeException(new RuntimeException(te)).getCause().getCause().getCause());
}

代码示例来源:origin: ReactiveX/RxJava

@Override
    public void onError(Throwable e) {
      String trace = stackTraceAsString(e);
      System.out.println("On Error: " + trace);

      assertTrue(trace, trace.contains("OnNextValue"));

      assertTrue("No Cause on throwable" + e, e.getCause() != null);
//            assertTrue(e.getCause().getClass().getSimpleName() + " no OnNextValue",
//                    e.getCause() instanceof OnErrorThrowable.OnNextValue);
    }

代码示例来源:origin: ReactiveX/RxJava

@Test
public void badException() {
  Throwable e = new BadException();
  assertSame(e, new CompositeException(e).getCause().getCause());
  assertSame(e, new CompositeException(new RuntimeException(e)).getCause().getCause().getCause());
}

代码示例来源:origin: ReactiveX/RxJava

@After
public void after() {
  RxJavaPlugins.reset();
  assertFalse("" + errors, errors.isEmpty());
  TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
  Throwable c = errors.get(0).getCause();
  assertTrue("" + c, c instanceof TestException);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void subscribeZeroError() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    assertTrue(Maybe.error(new TestException())
    .subscribe().isDisposed());
    TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
    Throwable c = errors.get(0).getCause();
    assertTrue("" + c, c instanceof TestException);
  } finally {
    RxJavaPlugins.reset();
  }
}

相关文章