Given the following incomplete method.
1. public void method(){
2.
3. if (someTestFails()){
4.
5. }
6.
7.}
You want to make this method throw an IOException if, and only if, the method someTestFails() returns a value of true. Which changes achieve this?
A.Add at line 2: IOException e;
B.Add at line 4: throw e;
C.Add at line 4: throw new IOException();
D.Add at line 6: throw new IOException();
E.Modify the method declaration to indicate that an object of [type] Exception might be thrown.
Answer given is (D), (E).
According to me answer should be (C) & (E).
(E) only if method being referred to is method(), not someTestFails(). Am i right??
Here is a strange thing that i am unable to understand. I tried the following code(though it may appear similar but it has nothing to do with the above question):
output is:
Inside catch block of func2()
Inside func1()
InterruptedException (at line 1) thrown inside catch block of func2 is not being propogated to default exception handler. However if the throws clause from header of main() and func1() is removed, compiler complains.
On the other hand if line 1 and line 2 are interchanged (i.e. instead of throwing exception at line 1, true value is returned and instead of returning a boolean value at line 2, exception is thrown), exception is being propagated to defalut exception handler as expected.
Output is:
Inside catch block of func2()
Exception in
thread "main" java.lang.InterruptedException: InterruptedException in catch block of func2()
at Qs.functionfailed(Qs.java:31)
at Qs.withinsomefunc(Qs.java:10)
at Qs.main(Qs.java:5)
Can anybody expalin me why is this happening?