Java : NoSuchElementException con ejemplos
NoSuchElementException (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de NoSuchElementException.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Lanzado por varios métodos de acceso para indicar que el elemento que se solicita no existe. (Traducción automática)
// Bad
final var list = List.<String>of();
try {
final var value = list.getFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
// Good
if (!list.isEmpty()) {
System.out.println("value : " + list.getFirst());
} else {
System.out.println("Empty!");
}
// Result
// ↓
//Empty!
// Bad
final var opt = Optional.<String>empty();
try {
final var value = opt.orElseThrow();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException! : " + e.getMessage());
}
// Result
// ↓
//NoSuchElementException! : No value present
// Good
if (opt.isPresent()) {
System.out.println("value : " + opt.orElseThrow());
} else {
System.out.println("Empty!");
}
// Result
// ↓
//Empty!
Constructors
NoSuchElementException ()
Construye una NoSuchElementException con nulo como cadena de mensaje de error. (Traducción automática)
final var e = new NoSuchElementException();
System.out.println(e); // java.util.NoSuchElementException
NoSuchElementException (String s)
Construye una NoSuchElementException y guarda una referencia a la cadena del mensaje de error para su posterior recuperación mediante el método getMessage. (Traducción automática)
final var e = new NoSuchElementException("abcd");
System.out.println(e); // java.util.NoSuchElementException: abcd
System.out.println(e.getMessage()); // abcd
NoSuchElementException (String s, Throwable cause)
Construye una NoSuchElementException con el mensaje detallado y la causa especificados. (Traducción automática)
final var cause = new IllegalStateException("XYZ");
final var e = new NoSuchElementException("abcd", cause);
System.out.println(e); // java.util.NoSuchElementException: abcd
System.out.println(e.getMessage()); // abcd
System.out.println(e.getCause()); // java.lang.IllegalStateException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
NoSuchElementException (Throwable cause)
Construye una NoSuchElementException con la causa especificada. (Traducción automática)
final var cause = new IllegalStateException("XYZ");
final var e = new NoSuchElementException(cause);
System.out.println(e); // java.util.NoSuchElementException: java.lang.IllegalStateException: XYZ
System.out.println(e.getMessage()); // java.lang.IllegalStateException: XYZ
System.out.println(e.getCause()); // java.lang.IllegalStateException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Consulte el siguiente enlace.
Related posts
- Ejemplos de API