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

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

Moderator: admin

Post Reply
bvanderhaar
Posts: 1
Joined: Sat Dec 15, 2012 2:49 pm
Contact:

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

Post by bvanderhaar »

If we reversed the order of the catch blocks like so:

catch(Exception e){
System.out.println("E");
}
catch(E1 e){
System.out.println("E1");
}


both E and E1 would print, correct? The explanation says "at the same level" which is confusing to me - like we are nesting try/catches.

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

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

Post by admin »

No, it will not because E1 is a subclass of Exception.

"At the same level" means exactly what you wrote above. i.e. Not nested. In the above code, catch(Exception e) and catch(E1 ) are at the same level. This is standard terminology. Once an exception is caught by a catch block, rest of the catch blocks at the same level are not invoked. In the code that you've written above, once E2 is caught by catch(Exception ...) block, catch(E1 ...) will not be invoked even though it can catch E2.

Think of it like putting a smaller bowl (i.e. a subclass E1) below a bigger one (superclass Exception). Any exception that can be caught by the bigger bowl, will be caught by the bigger bowl [catch(Exception ...) can catch Exception as well as E1 as well as E2] and there will be nothing left for the smaller one. So catch(E1 ...) will not be executed.

But if you have smaller bowl (E1) on top of the bigger bowl (Exception), any exception that can be caught by the smaller bowl (E1) will be caught by catch(E1) first and any left overs can then be caught by the bigger bowl below. Note that I am calling catch(E1) as a smaller bowl than catch(Exception ) because catch(E1) can only catch E1 and subclasses of E1, while catch(Exception ) can catch Exception and any subclasses of Exception which includes E1 and E2.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post by Daniel Clinton »

bvanderhaar wrote:If we reversed the order of the catch blocks like so:

catch(Exception e){
System.out.println("E");
}
catch(E1 e){
System.out.println("E1");
}


both E and E1 would print, correct? The explanation says "at the same level" which is confusing to me - like we are nesting try/catches.
admin wrote:No, it will not because E1 is a subclass of Exception.

"At the same level" means exactly what you wrote above. i.e. Not nested. In the above code, catch(Exception e) and catch(E1 ) are at the same level. This is standard terminology. Once an exception is caught by a catch block, rest of the catch blocks at the same level are not invoked. In the code that you've written above, once E2 is caught by catch(Exception ...) block, catch(E1 ...) will not be invoked even though it can catch E2.

Think of it like putting a smaller bowl (i.e. a subclass E1) below a bigger one (superclass Exception). Any exception that can be caught by the bigger bowl, will be caught by the bigger bowl [catch(Exception ...) can catch Exception as well as E1 as well as E2] and there will be nothing left for the smaller one. So catch(E1 ...) will not be executed.

But if you have smaller bowl (E1) on top of the bigger bowl (Exception), any exception that can be caught by the smaller bowl (E1) will be caught by catch(E1) first and any left overs can then be caught by the bigger bowl below. Note that I am calling catch(E1) as a smaller bowl than catch(Exception ) because catch(E1) can only catch E1 and subclasses of E1, while catch(Exception ) can catch Exception and any subclasses of Exception which includes E1 and E2.

HTH,
Paul.
Wondering why it is not mentioned in the explanation that this code tweak
of bvanderhaar's wouldn't compile
- admittedly for the same larger-bowl-cannot-go-on-top reason :)

TestClass6.java:12: error: exception E1 has already been caught

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

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

Post by admin »

I probably didn't even check if it would compile or not when I wrote the reply because I was more focused on clearing OP's main issue.
If you like our products and services, please help us by posting your review here.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post by Daniel Clinton »

Sorry there Paul
I'm not meaning to pick holes,
just trying to make sure I don't miss something in my understanding of each question
The descending bowls are great memory aid btw

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

I think this question is not in scope for the exam because it is about making your own Exception class?

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

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

Post by admin »

The question doesn't ask you to create your own exception class at all! It is squarely in scope of the exam.
If you like our products and services, please help us by posting your review here.

SL____
Posts: 3
Joined: Fri Jul 26, 2019 1:12 am
Contact:

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

Post by SL____ »

Test answer is not correct. Or, it doesn't fit into multiple answer test type. Given
"It will print E1 and Finally."
is valid, then these two must be valid as well:
"It will print E and Finally."
"It will print Finally."
Just because literally, they will be printed.

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

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

Post by admin »

Since the question asks you to pick only 1 option, you need to select the best option. Option 2, "It will print E1 and Finally" is the best option because that is what is actually printed.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 27 guests