-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestException.java
30 lines (24 loc) · 991 Bytes
/
TestException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Explanation:
What is the problem with below program?
Ans: This program will give compilation error with the message as "The exception FileNotFoundException is already caught by the alternative IOException".
This is due to FileNotFoundException is a child class of IOException
Recommend a solution:
Ans: Move the exceptions into separate exception blocks. catch the more specific FileNotFoundException first and then try to catch for IOException.
The below code is the correct version of the problem statement.
*/
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestException {
public static void main(String[] args) {
try {
testExceptions();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void testExceptions() throws IOException, FileNotFoundException {
}
}