public class ExceptionHandler {
public static void main(String[] args) throws FileNotFoundException, IOException {
// write your code here
try {
testException(-5);
testException(11);
} catch (FileNotFoundException e){
System.out.println("No File Found");
} catch (IOException e){
System.out.println("IO Error occurred");
} finally { //The finally block always executes when the try block exits.
System.out.println("Releasing resources");
testException(15);
}
}
public static void testException(int i) throws FileNotFoundException, IOException {
if (i < 0) {
FileNotFoundException myException = new FileNotFoundException();
throw myException;
}
else if (i > 10) {
throw new IOException();
}
}
}
The output of this code gives
No File Found
Releasing resources
Is it possible to have java catch both IOException as well as FileNotFoundException? It seems to be only able to catch the first exception and doesn't catch the IOException