About Question enthuware.ocajp.i.v7.2.1005 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1005 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post by admin »

Did you read the explanation?
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post 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....

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post by fasty23 »

If we use a try catch block in doB, does the main method need throws exception?
tnx

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post by admin »

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.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post 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. :)

Sai Divya sree
Posts: 14
Joined: Mon Jun 20, 2016 11:16 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post by admin »

What happened when you tried to compile it?
If you like our products and services, please help us by posting your review here.

asi-aal
Posts: 10
Joined: Wed Nov 23, 2022 3:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1005 :

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 55 guests