Given the code fragment:
Which two modifications should you make so that the code compiles successfully?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Given the code fragment:
Which two modifications should you make so that the code compiles successfully?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
firstly, to work with IOException, it is needed to add a line in the beginning of that program “import java.io.IOException;” , otherwise compiler will not understand, what is IOException. With “import java.io.IOException;” added, result will be following:
#javac Test37.java
Test37.java:5: error: unreported exception IOException; must be caught or declared to be thrown
throw new IOException();
^
1 error
Which gives a hint that on line 5 it is needed to trow IOException (answer B). Then the same should be done for the main method (answer C)
Answer B, C:
(B): A checked exception is thrown, meaning that it should be handled (by try/catch) or declared. Answer B solves this problem.
(C): The caller method should also declare or handle the exception thrown by the called method. Answer C solves this problem.
(D) is not correct because we have two catch blocks, and the preceding one is catching an exception that is a super class of the one caught in the consequent catch block.
Other options are non-sense.
Answers are BC
D is incorrect because “Unreachable catch block for IOException. It is already handled by the catch block for Exception”
class X{
public void printFileConent(){
throw new IOException();
}
}
public class Test {
public static void main(String[] args) {
X xobj = new X();
xobj.printFileConent();
}
}
BC
D is wrong cause IOException is already catched by Exception