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

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

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

Exception.getCause介绍

暂无

代码示例

代码示例来源:origin: org.mockito/mockito-core

private static String exceptionCauseMessageIfAvailable(Exception details) {
  if (details.getCause() == null) {
    return details.getMessage();
  }
  return details.getCause().getMessage();
}

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

private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception {
 Throwable cause = e.getCause();
 if (cause == null) {
  throw e;
 }
 if (combineStackTraces) {
  StackTraceElement[] combined =
    ObjectArrays.concat(cause.getStackTrace(), e.getStackTrace(), StackTraceElement.class);
  cause.setStackTrace(combined);
 }
 if (cause instanceof Exception) {
  throw (Exception) cause;
 }
 if (cause instanceof Error) {
  throw (Error) cause;
 }
 // The cause is a weird kind of Throwable, so throw the outer exception.
 throw e;
}

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

/**
 * Find a {@link Method} to handle the given exception.
 * Use {@link ExceptionDepthComparator} if more than one match is found.
 * @param exception the exception
 * @return a Method to handle the exception, or {@code null} if none found
 */
@Nullable
public Method resolveMethod(Exception exception) {
  Method method = resolveMethodByExceptionType(exception.getClass());
  if (method == null) {
    Throwable cause = exception.getCause();
    if (cause != null) {
      method = resolveMethodByExceptionType(cause.getClass());
    }
  }
  return method;
}

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

private static <X extends Exception> X newWithCause(Class<X> exceptionClass, Throwable cause) {
 // getConstructors() guarantees this as long as we don't modify the array.
 @SuppressWarnings({"unchecked", "rawtypes"})
 List<Constructor<X>> constructors = (List) Arrays.asList(exceptionClass.getConstructors());
 for (Constructor<X> constructor : preferringStrings(constructors)) {
  @Nullable X instance = newFromConstructor(constructor, cause);
  if (instance != null) {
   if (instance.getCause() == null) {
    instance.initCause(cause);
   }
   return instance;
  }
 }
 throw new IllegalArgumentException(
   "No appropriate constructor for exception of type "
     + exceptionClass
     + " in response to chained exception",
   cause);
}

代码示例来源:origin: apache/kafka

@Override
  public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception exception) {
    responses.incrementAndGet();
    Throwable cause = exception.getCause();
    assertTrue("Unexpected exception cause type: " + (cause == null ? null : cause.getClass()),
        cause instanceof DisconnectException);
  }
});

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

@Override
 public TestException apply(Exception from) {
  if (from instanceof ExecutionException) {
   return new TestException(from.getCause());
  } else {
   assertTrue(
     "got " + from.getClass(),
     from instanceof InterruptedException || from instanceof CancellationException);
   return new TestException(from);
  }
 }
};

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

@Test
  @Ignore("Observers can't throw")
  public void onErrorNotImplementedFailureSafe() {
    try {
      new SafeObserver<String>(OBSERVER_ONERROR_NOTIMPLEMENTED()).onError(new SafeObserverTestException("error!"));
      fail("expects exception to be thrown");
    } catch (Exception e) {
//            assertTrue(e instanceof OnErrorNotImplementedException);
      assertTrue(e.getCause() instanceof SafeObserverTestException);
      assertEquals("error!", e.getCause().getMessage());
    }
  }

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

@Test
  @Ignore("Observers can't throw")
  public void onErrorNotImplementedFailureSafe() {
    try {
      new SafeSubscriber<String>(subscriberOnErrorNotImplemented()).onError(new SafeSubscriberTestException("error!"));
      fail("expects exception to be thrown");
    } catch (Exception e) {
//            assertTrue(e instanceof OnErrorNotImplementedException);
      assertTrue(e.getCause() instanceof SafeSubscriberTestException);
      assertEquals("error!", e.getCause().getMessage());
    }
  }

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

@Test
  @Ignore("Observers can't throw")
  public void onCompleteSuccessWithUnsubscribeFailure() {
    Subscriber<String> subscriber = subscriberSuccess();
    try {
      subscriber.onSubscribe(THROWING_DISPOSABLE);
      new SafeSubscriber<String>(subscriber).onComplete();
      fail("expects exception to be thrown");
    } catch (Exception e) {
      e.printStackTrace();

      // FIXME no longer assertable
//            assertTrue(o.isUnsubscribed());
//            assertTrue(e instanceof UnsubscribeFailedException);
      assertTrue(e.getCause() instanceof SafeSubscriberTestException);
      assertEquals("failure from unsubscribe", e.getMessage());
      // expected since onError fails so SafeSubscriber can't help
    }
  }

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
  public void testWithoutMessage() throws Exception {
    final Exception exception = new CloneFailedException(generateCause());
    assertNotNull(exception);
    assertNotNull(exception.getMessage());

    final Throwable cause = exception.getCause();
    assertNotNull(cause);
    assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage());
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testWithCauseAndMessage() throws Exception {
  final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE, generateCause());
  assertNotNull(exception);
  assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage());
  final Throwable cause = exception.getCause();
  assertNotNull(cause);
  assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage());
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
  public void testWithoutMessage() throws Exception {
    final Exception exception = new CircuitBreakingException(generateCause());
    assertNotNull(exception);
    assertNotNull(exception.getMessage());

    final Throwable cause = exception.getCause();
    assertNotNull(cause);
    assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage());
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testWithCauseAndMessage() throws Exception {
  final Exception exception = new CircuitBreakingException(EXCEPTION_MESSAGE, generateCause());
  assertNotNull(exception);
  assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage());
  final Throwable cause = exception.getCause();
  assertNotNull(cause);
  assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage());
}

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

@Test
@Ignore("Observers can't throw")
public void onNextOnErrorFailureSafe() {
  try {
    new SafeSubscriber<String>(OBSERVER_ONNEXT_ONERROR_FAIL()).onNext("one");
    fail("expects exception to be thrown");
  } catch (Exception e) {
    e.printStackTrace();
    assertTrue(e instanceof RuntimeException);
    assertEquals("Error occurred when trying to propagate error to Subscriber.onError", e.getMessage());
    Throwable e2 = e.getCause();
    assertTrue(e2 instanceof CompositeException);
    List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions();
    assertEquals(2, innerExceptions.size());
    Throwable e3 = innerExceptions.get(0);
    assertTrue(e3 instanceof SafeSubscriberTestException);
    assertEquals("onNextFail", e3.getMessage());
    Throwable e4 = innerExceptions.get(1);
    assertTrue(e4 instanceof SafeSubscriberTestException);
    assertEquals("onErrorFail", e4.getMessage());
  }
}

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

@Test
@Ignore("Observers can't throw")
public void onNextOnErrorFailureSafe() {
  try {
    new SafeObserver<String>(OBSERVER_ONNEXT_ONERROR_FAIL()).onNext("one");
    fail("expects exception to be thrown");
  } catch (Exception e) {
    e.printStackTrace();
    assertTrue(e instanceof RuntimeException);
    assertEquals("Error occurred when trying to propagate error to Observer.onError", e.getMessage());
    Throwable e2 = e.getCause();
    assertTrue(e2 instanceof CompositeException);
    List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions();
    assertEquals(2, innerExceptions.size());
    Throwable e3 = innerExceptions.get(0);
    assertTrue(e3 instanceof SafeObserverTestException);
    assertEquals("onNextFail", e3.getMessage());
    Throwable e4 = innerExceptions.get(1);
    assertTrue(e4 instanceof SafeObserverTestException);
    assertEquals("onErrorFail", e4.getMessage());
  }
}

代码示例来源:origin: apache/kafka

@Test
public void testSetAttributes() throws Exception {
  try {
    mBeanServer.setAttributes(objectName(countMetricName), new AttributeList(1));
    fail("setAttributes should have failed");
  } catch (Exception e) {
    assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
  }
}

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

@Test
@Ignore("Observers can't throw")
public void onErrorFailureSafe() {
  try {
    new SafeSubscriber<String>(subscriberOnErrorFail()).onError(new SafeSubscriberTestException("error!"));
    fail("expects exception to be thrown");
  } catch (Exception e) {
    e.printStackTrace();
    assertTrue(e instanceof RuntimeException);
    assertEquals("Error occurred when trying to propagate error to Subscriber.onError", e.getMessage());
    Throwable e2 = e.getCause();
    assertTrue(e2 instanceof CompositeException);
    List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions();
    assertEquals(2, innerExceptions.size());
    Throwable e3 = innerExceptions.get(0);
    assertTrue(e3 instanceof SafeSubscriberTestException);
    assertEquals("error!", e3.getMessage());
    Throwable e4 = innerExceptions.get(1);
    assertTrue(e4 instanceof SafeSubscriberTestException);
    assertEquals("onErrorFail", e4.getMessage());
  }
}

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

@Test
@Ignore("Observers can't throw")
public void onErrorFailureSafe() {
  try {
    new SafeObserver<String>(OBSERVER_ONERROR_FAIL()).onError(new SafeObserverTestException("error!"));
    fail("expects exception to be thrown");
  } catch (Exception e) {
    e.printStackTrace();
    assertTrue(e instanceof RuntimeException);
    assertEquals("Error occurred when trying to propagate error to Observer.onError", e.getMessage());
    Throwable e2 = e.getCause();
    assertTrue(e2 instanceof CompositeException);
    List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions();
    assertEquals(2, innerExceptions.size());
    Throwable e3 = innerExceptions.get(0);
    assertTrue(e3 instanceof SafeObserverTestException);
    assertEquals("error!", e3.getMessage());
    Throwable e4 = innerExceptions.get(1);
    assertTrue(e4 instanceof SafeObserverTestException);
    assertEquals("onErrorFail", e4.getMessage());
  }
}

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

@Test
  @Ignore("Observers can't throw")
  public void onCompleteSuccessWithUnsubscribeFailure() {
    Observer<String> o = OBSERVER_SUCCESS();
    try {
      o.onSubscribe(THROWING_DISPOSABLE);
      new SafeObserver<String>(o).onComplete();
      fail("expects exception to be thrown");
    } catch (Exception e) {
      e.printStackTrace();

      // FIXME no longer assertable
//            assertTrue(o.isUnsubscribed());
//            assertTrue(e instanceof UnsubscribeFailedException);
      assertTrue(e.getCause() instanceof SafeObserverTestException);
      assertEquals("failure from unsubscribe", e.getMessage());
      // expected since onError fails so SafeObserver can't help
    }
  }

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

@Test
public void afterCompletionWithPreSendException() {
  PreSendInterceptor interceptor1 = new PreSendInterceptor();
  PreSendInterceptor interceptor2 = new PreSendInterceptor();
  interceptor2.setExceptionToRaise(new RuntimeException("Simulated exception"));
  this.channel.addInterceptor(interceptor1);
  this.channel.addInterceptor(interceptor2);
  try {
    this.channel.send(MessageBuilder.withPayload("test").build());
  }
  catch (Exception ex) {
    assertEquals("Simulated exception", ex.getCause().getMessage());
  }
  assertTrue(interceptor1.wasAfterCompletionInvoked());
  assertFalse(interceptor2.wasAfterCompletionInvoked());
}

相关文章