Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Tue Mar 05, 2013 12:39 pm
by ETS User
Hi,
The program should compile if you only add throws Exception in method signature doB() at line //2, for this reason option B should be the correct answer not D.
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Tue Mar 05, 2013 1:32 pm
by admin
Did you read the explanation?
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Tue Mar 05, 2013 1:48 pm
by Guest
admin wrote:Did you read the explanation?
I understand that including throws in method signature forces the caller to either handle or declare but the question was asking whether the program would
compile right? and it would if you just put throws in line //2. What do you think?
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Tue Mar 05, 2013 4:44 pm
by admin
It would still not compile because the main method would also have to catch or throw the exception, which is declared in the throws clause of the called method. That's what the explanation explains.
You might want to try it out and confirm.
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Tue Mar 05, 2013 6:34 pm
by Guest
admin wrote:It would still not compile because the main method would also have to catch or throw the exception, which is declared in the throws clause of the called method. That's what the explanation explains.
You might want to try it out and confirm.
you win brother....
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Sat Mar 08, 2014 11:19 pm
by fasty23
If we use a try catch block in doB, does the main method need throws exception?
tnx
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Sat Mar 08, 2014 11:26 pm
by admin
Please post the code to show what you mean and what happens when you compile and run it.
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Sun Mar 09, 2014 11:33 am
by fasty23
admin wrote:Please post the code to show what you mean and what happens when you compile and run it.
I tried this code:
public class A {
public void doA(int k) throws Exception { // 0
for(int i=0; i< 10; i++) {
if(i == k) throw new Exception("Index of k is "+i); // 1
}
}
public void doB(boolean f) { // 2
try {
if(f) {
doA(15); // 3
}
else return;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) { // 4
A a = new A();
a.doB(args.length>0); // 5
}
}
and it worked, the main method does not need throws exception any more when we surround if else statement in doB method by try catch block.
Thank you for guide.

Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Wed Jul 06, 2016 6:47 pm
by Sai Divya sree
Code: Select all
class A {
public void doA(int k) throws Exception { // 0
for(int i=0; i< 10; i++) {
if(i == k) throw new Exception("Index of k is "+i); // 1
}
}
public void doB(boolean f) { // 2
if(f) {
doA(15); // 3
}
else return;
}
public static void main(String[] args) { // 4
A a = new A();
a.doB(args.length>0); // 5
}
}
In the above program when doA(15) is called ..K is initialised to 15.since in the for loop
for(int i=0; i< 10; i++) {
if(i == k) throw new Exception("Index of k is "+i); // 1
}
'i' can never be 15 i.e (i==k) is not satisfied so it doesn't throw any exception.Even when it doesn't throw exception should we follow the handle or declare rule for doB() and main() methods?
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Wed Jul 06, 2016 6:59 pm
by admin
What happened when you tried to compile it?
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Posted: Wed Nov 30, 2022 5:06 pm
by asi-aal
fasty23 wrote: ↑Sat Mar 08, 2014 11:19 pm
If we use a try catch block in doB, does the main method need throws exception?
tnx
Even if // 1 is enclosed in a try block, the method still has
throws Exception in its declaration, which will force the caller of this method to either declare Exception in its throws clause or put the call within a try block.