About Question enthuware.ocajp.i.v7.2.1005 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1005 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Did you read the explanation?
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1005 :
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?admin wrote:Did you read the explanation?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
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 might want to try it out and confirm.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1005 :
you win brother....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.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
If we use a try catch block in doB, does the main method need throws exception?
tnx
tnx
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
Please post the code to show what you mean and what happens when you compile and run it.
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
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.
-
- Posts: 14
- Joined: Mon Jun 20, 2016 11:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
What happened when you tried to compile it?
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Wed Nov 23, 2022 3:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1005 :
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.
Who is online
Users browsing this forum: No registered users and 99 guests