Java : IOException with Examples
IOException (Java SE 22 & JDK 22) in Java with Examples.
You will find code samples for most of the IOException methods.
Summary
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
System.out.println(Files.notExists(path)); // true
try {
final var str = Files.readString(path);
} catch (IOException e) {
System.out.println(e);
}
// Result
// ↓
//java.nio.file.NoSuchFileException: R:\java-work\aaa.txt
Constructors
IOException ()
final var e = new IOException();
System.out.println(e); // java.io.IOException
IOException (String message)
final var e = new IOException("abcd");
System.out.println(e); // java.io.IOException: abcd
System.out.println(e.getMessage()); // abcd
IOException (String message, Throwable cause)
final var cause = new IOException("XYZ");
final var e = new IOException("abcd", cause);
System.out.println(e); // java.io.IOException: abcd
System.out.println(e.getMessage()); // abcd
System.out.println(e.getCause()); // java.io.IOException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
IOException (Throwable cause)
final var cause = new IOException("XYZ");
final var e = new IOException(cause);
System.out.println(e); // java.io.IOException: java.io.IOException: XYZ
System.out.println(e.getCause()); // java.io.IOException: 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
Please see the link below.
Related posts
- API Examples
- Error
- Exception
- RuntimeException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- CancellationException
- ClassCastException
- ConcurrentModificationException
- DateTimeException
- DateTimeParseException
- IllegalArgumentException
- IllegalStateException
- IndexOutOfBoundsException
- NoSuchElementException
- NullPointerException
- PatternSyntaxException
- StringIndexOutOfBoundsException
- UnsupportedOperationException
- Throwable