Exception Handling in Java
try/catch/finally, checked vs unchecked exceptions and try-with-resources.
Easy
try (var reader = new BufferedReader(new FileReader("data.csv"))) { // closed automatically
String line = reader.readLine();
} catch (IOException e) {
System.err.println("Could not read: " + e.getMessage());
}
- Checked exceptions (
IOException) must be caught or declared withthrows. - Unchecked exceptions (
NullPointerException,IllegalArgumentException) usually signal programming bugs.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.