java.lang.Exception类的使用及代码示例

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

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

Exception介绍

[英]The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
[中]类异常及其子类是Throwable的一种形式,表示合理的应用程序可能希望捕获的条件。
类异常和不属于RuntimeException子类的任何子类都是检查异常。如果检查的异常可以通过方法或构造函数的执行抛出并传播到方法或构造函数边界之外,则需要在方法或构造函数的throws子句中声明。

代码示例

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

@Override
public List<String> getChildren(String path) {
  try {
    return client.getChildren().forPath(path);
  } catch (NoNodeException e) {
    return null;
  } catch (Exception e) {
    throw new IllegalStateException(e.getMessage(), e);
  }
}

代码示例来源:origin: square/okhttp

@Override protected void execute() {
  try {
   drainQueue();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
});

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

@Override
  public Object apply(Long aLong) throws Exception {
    throw new Exception();
  }
});

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

private Exception test() {
    Exception e = new Exception();
    try {
      Thread.sleep(50);
    } catch (Exception ex) {
      e = ex;
    }
    e.printStackTrace();
    return e;
  }
}

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

public void testCheckedThrowableSync(CacheableService<?> service) throws Exception {
  String arg = UUID.randomUUID().toString();
  try {
    service.throwCheckedSync(arg);
    fail("Excepted exception");
  }
  catch (Exception ex) {
    ex.printStackTrace();
    assertEquals("Wrong exception type", IOException.class, ex.getClass());
    assertEquals(arg, ex.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: spring-projects/spring-framework

@Test
public void handleClientMessageProcessingError() throws Exception {
  Exception ex = new Exception("fake exception");
  Message<byte[]> actual = this.handler.handleClientMessageProcessingError(null, ex);
  StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(actual, StompHeaderAccessor.class);
  assertNotNull(accessor);
  assertEquals(StompCommand.ERROR, accessor.getCommand());
  assertEquals(ex.getMessage(), accessor.getMessage());
  assertArrayEquals(new byte[0], actual.getPayload());
}

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

public void testGetRootCause_Loop() {
 Exception cause = new Exception();
 Exception exception = new Exception(cause);
 cause.initCause(exception);
 try {
  Throwables.getRootCause(cause);
  fail("Should have throw IAE");
 } catch (IllegalArgumentException expected) {
  assertThat(expected).hasCauseThat().isSameAs(cause);
 }
}

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

private void assertIsCompiled(Expression expression) {
  try {
    Field field = SpelExpression.class.getDeclaredField("compiledAst");
    field.setAccessible(true);
    Object object = field.get(expression);
    assertNotNull(object);
  }
  catch (Exception ex) {
    fail(ex.toString());
  }
}

代码示例来源: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: skylot/jadx

public ClassNode getClassNode(Class<?> clazz) {
  try {
    File jar = getJarForClass(clazz);
    return getClassNodeFromFile(jar, clazz.getName());
  } catch (Exception e) {
    e.printStackTrace();
    fail(e.getMessage());
  }
  return null;
}

代码示例来源: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: google/guava

public void testGetCasualChainLoop() {
 Exception cause = new Exception();
 Exception exception = new Exception(cause);
 cause.initCause(exception);
 try {
  Throwables.getCausalChain(cause);
  fail("Should have throw IAE");
 } catch (IllegalArgumentException expected) {
  assertThat(expected).hasCauseThat().isSameAs(cause);
 }
}

代码示例来源: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: apache/incubator-dubbo

@Override
public String doGetContent(String path) {
  try {
    byte[] dataBytes = client.getData().forPath(path);
    return (dataBytes == null || dataBytes.length == 0) ? null : new String(dataBytes, charset);
  } catch (NoNodeException e) {
    // ignore NoNode Exception.
  } catch (Exception e) {
    throw new IllegalStateException(e.getMessage(), e);
  }
  return null;
}

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

protected void generateClsCode(ClassNode cls) {
  try {
    new CodeGen().visit(cls);
  } catch (Exception e) {
    e.printStackTrace();
    fail(e.getMessage());
  }
}

代码示例来源:origin: JakeWharton/butterknife

private void logParsingError(Element element, Class<? extends Annotation> annotation,
  Exception e) {
 StringWriter stackTrace = new StringWriter();
 e.printStackTrace(new PrintWriter(stackTrace));
 error(element, "Unable to parse @%s binding.\n\n%s", annotation.getSimpleName(), stackTrace);
}

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

@Override
  public Object apply(Long aLong) throws Exception {
    throw new Exception();
  }
});

代码示例来源: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: spring-projects/spring-framework

@Override
  public String handleException(Exception exception) {
    return exception.getMessage();
  }
}

相关文章