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

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

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

IllegalArgumentException介绍

[英]Thrown to indicate that a method has been passed an illegal or inappropriate argument.
[中]抛出以指示已向方法传递非法或不适当的参数。

代码示例

代码示例来源:origin: stackoverflow.com

if (string.contains("-")) {
  // Split it.
} else {
  throw new IllegalArgumentException("String " + string + " does not contain -");
}

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

private void assertCastFails(long value) {
 try {
  Chars.checkedCast(value);
  fail("Cast to char should have failed: " + value);
 } catch (IllegalArgumentException ex) {
  assertTrue(
    value + " not found in exception text: " + ex.getMessage(),
    ex.getMessage().contains(String.valueOf(value)));
 }
}

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

private static void assertCastFails(long value) {
 try {
  Shorts.checkedCast(value);
  fail("Cast to short should have failed: " + value);
 } catch (IllegalArgumentException ex) {
  assertTrue(
    value + " not found in exception text: " + ex.getMessage(),
    ex.getMessage().contains(String.valueOf(value)));
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

@Override
public void onPreCall() {
 if (n < 0) {
  throw new IllegalArgumentException("n is less than 0");
 }
}

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

private static void assertCastFails(long value) {
 try {
  SignedBytes.checkedCast(value);
  fail("Cast to byte should have failed: " + value);
 } catch (IllegalArgumentException ex) {
  assertTrue(
    value + " not found in exception text: " + ex.getMessage(),
    ex.getMessage().contains(String.valueOf(value)));
 }
}

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

private static void checkPercentageValidity(int percentage, String message) {
  if (percentage < 0 || percentage > 100) {
   throw new IllegalArgumentException(message);
  }
 }
}

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

public void testToOptionalMultiple() {
 try {
  Stream.of(1, 2).collect(MoreCollectors.toOptional());
  fail("Expected IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
  assertThat(expected.getMessage()).contains("1, 2");
 }
}

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

public static int checkDuration(String name, long duration, TimeUnit unit) {
 if (duration < 0) throw new IllegalArgumentException(name + " < 0");
 if (unit == null) throw new NullPointerException("unit == null");
 long millis = unit.toMillis(duration);
 if (millis > Integer.MAX_VALUE) throw new IllegalArgumentException(name + " too large.");
 if (millis == 0 && duration > 0) throw new IllegalArgumentException(name + " too small.");
 return (int) millis;
}

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

/** Can't create the map if more than one value maps to the same key. */
public void testUniqueIndexDuplicates() {
 try {
  Map<Integer, String> unused =
    Maps.uniqueIndex(ImmutableSet.of("one", "uno"), Functions.constant(1));
  fail();
 } catch (IllegalArgumentException expected) {
  assertThat(expected.getMessage()).contains("Multimaps.index");
 }
}

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

static void checkNotPrimitive(Type type) {
 if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) {
  throw new IllegalArgumentException();
 }
}

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

public void testOnlyElementMany() {
  try {
   Stream.of(1, 2, 3, 4, 5, 6).collect(MoreCollectors.onlyElement());
   fail("Expected IllegalArgumentException");
  } catch (IllegalArgumentException expected) {
   assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ...");
  }
 }
}

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

static Type getParameterUpperBound(int index, ParameterizedType type) {
 Type[] types = type.getActualTypeArguments();
 if (index < 0 || index >= types.length) {
  throw new IllegalArgumentException(
    "Index " + index + " not in range [0," + types.length + ") for " + type);
 }
 Type paramType = types[index];
 if (paramType instanceof WildcardType) {
  return ((WildcardType) paramType).getUpperBounds()[0];
 }
 return paramType;
}

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

@SuppressWarnings("unchecked")
@Test
public void badCapacityHint() throws Exception {
  Observable<Integer> source = Observable.just(1);
  try {
    Observable.concatEager(Arrays.asList(source, source, source), 1, -99);
  } catch (IllegalArgumentException ex) {
    assertEquals("prefetch > 0 required but it was -99", ex.getMessage());
  }
}

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

@Override public void setFixedLengthStreamingMode(long contentLength) {
 if (super.connected) throw new IllegalStateException("Already connected");
 if (chunkLength > 0) throw new IllegalStateException("Already in chunked mode");
 if (contentLength < 0) throw new IllegalArgumentException("contentLength < 0");
 this.fixedContentLength = contentLength;
 super.fixedContentLength = (int) Math.min(contentLength, Integer.MAX_VALUE);
}

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

public void testOfWithDuplicateKey() {
 try {
  ImmutableBiMap.of("one", 1, "one", 1);
  fail();
 } catch (IllegalArgumentException expected) {
  assertThat(expected.getMessage()).contains("one");
 }
}

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

static RuntimeException methodError(Method method, @Nullable Throwable cause, String message,
  Object... args) {
 message = String.format(message, args);
 return new IllegalArgumentException(message
   + "\n    for method "
   + method.getDeclaringClass().getSimpleName()
   + "."
   + method.getName(), cause);
}

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

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void mappingBadCapacityHint() throws Exception {
  Observable<Integer> source = Observable.just(1);
  try {
    Observable.just(source, source, source).concatMapEager((Function)Functions.identity(), 10, -99);
  } catch (IllegalArgumentException ex) {
    assertEquals("prefetch > 0 required but it was -99", ex.getMessage());
  }
}

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

@Override
  public boolean test(Integer t) throws Exception {
    throw new IllegalArgumentException();
  }
});

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

@Test
public void takeNegative() {
  try {
    Observable.just(1).take(-99);
    fail("Should have thrown");
  } catch (IllegalArgumentException ex) {
    assertEquals("count >= 0 required but it was -99", ex.getMessage());
  }
}

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

@Override
  public Integer apply(Integer t1) {
    throw new IllegalArgumentException("some error");
  }
}).blockingSingle();

相关文章