Page 1 of 1

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

Posted: Sun Dec 16, 2012 4:18 pm
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.

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

Posted: Sun Dec 16, 2012 5:23 pm
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.

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

Posted: Mon Oct 27, 2014 12:33 pm
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

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

Posted: Mon Oct 27, 2014 12:44 pm
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.

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

Posted: Mon Oct 27, 2014 1:24 pm
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

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

Posted: Sun Jul 22, 2018 10:24 am
by flex567
I think this question is not in scope for the exam because it is about making your own Exception class?

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

Posted: Mon Jul 23, 2018 4:30 am
by admin
The question doesn't ask you to create your own exception class at all! It is squarely in scope of the exam.

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

Posted: Tue Aug 06, 2019 12:28 am
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.

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

Posted: Tue Aug 06, 2019 12:41 am
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.