Can you catch multiple exceptions in one catch block in Java?
Hello everyone! I started learning Java recently and encountered a problem with exception handling. I have a method that reads a file and parses numbers from strings. I want to properly handle several possible errors: if the file is not found (FileNotFoundException), if an input/output error occurs (IOException), and if the string is not a number (NumberFormatException).
Currently my code looks like this:
```java
try {
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
String line = reader.readLine();
int number = Integer.parseInt(line);
System.out.println(number);
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Input/output error: " + e.getMessage());
}
No answers yet.
Log in to leave an answer
Blogs