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

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.826 :

Post by ETS User »

According to the explanation, when it uses "System.out.println(exception)", a track trace is not printed and just the name of the exception class and the message is printed so... why is the option A and not the option C???
In option A, if I am not wrong, a track trace is printed, isn't?


Thanks in advance.

dtchky
Posts: 19
Joined: Wed Aug 01, 2012 3:11 am
Contact:

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

Post by dtchky »

Code: Select all

        int[] array = new int[10];
        array[10] = 1000;
The code above throws an ArrayIndexOutOfBoundsException before it ever reaches doAnotherTest, thus the answer is A and not C - the program is not throwing "MyException" since it crashes out before it reaches that piece of the code.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post by javanaut »

ETS User wrote:According to the explanation, when it uses "System.out.println(exception)", a track trace is not printed and just the name of the exception class and the message is printed so... why is the option A and not the option C???
In option A, if I am not wrong, a track trace is printed, isn't?


Thanks in advance.
Yes, I was disturbed by this as well. The explanation says that a stack trace is not printed but if I comment out the System.out.println(me) and add me.printStackTrace() the exact same output is printed to the console.

This definitely needs to be edited as far as explaining the answer since a stack trace is printed and not just the name of the exception class and its string argument.

:shock: :o

Regards,

Javanaut

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

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

Post by admin »

Hello,
The explanation is correct. The stack trace is being printed here NOT because of the println (which is never even invoked) but because the exception thrown at array[10] = 1000 is never caught by the catch block and is propagated out of the main method. It is the JVM's uncaught exception handler that prints the stack trace.

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

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post by javanaut »

Hi Paul,

Ah, yes this explanation makes sense. Thank-you for explaining this. That is tricky.

Regards,

Javanaut

yeffayeffa
Posts: 2
Joined: Tue Jan 07, 2014 1:57 am
Contact:

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

Post by yeffayeffa »

admin wrote:Hello,
The explanation is correct. The stack trace is being printed here NOT because of the println (which is never even invoked) but because the exception thrown at array[10] = 1000 is never caught by the catch block and is propagated out of the main method. It is the JVM's uncaught exception handler that prints the stack trace.

HTH,
Paul.
Hallo, i dont't understand why ArrayIndexOutOfBounds Exception is never by caught by MyException. MyException extends Exception so it could intercept any exception but probably ii'm wrong doing this reasoning.

thanks

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

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

Post by admin »

MyException extends Exception but ArrayIndexOutOfBounds doesn't extend MyException. In other words, ArrayIndexOutOfBounds is-not-a MyException and so it cannot be caught by catch(MyException).
To know if a handler can catch a particular exception, you can check whether the exception that you want to catch satisfies the is-a test. For example, if you have catch(ExA a), and if the exception that is thrown is of class ExB, then ExB will be caught by the catch only if ExB is-a ExA is satisfied.

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

yeffayeffa
Posts: 2
Joined: Tue Jan 07, 2014 1:57 am
Contact:

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

Post by yeffayeffa »

ok,now it's very clear. i was missing the "is-a" relationship. But ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundException that is a subclass of RunTimeException that is a subclass of Exception. So ArrayIndexOutOfBoundsException is not a subclass of Exception ?

apanah
Posts: 5
Joined: Sat Jun 21, 2014 3:49 pm
Contact:

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

Post by apanah »

yeffayeffa wrote:ok,now it's very clear. i was missing the "is-a" relationship. But ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundException that is a subclass of RunTimeException that is a subclass of Exception. So ArrayIndexOutOfBoundsException is not a subclass of Exception ?
I had the same thought - can you respond on this?

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

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

Post by admin »

Please see attached class diagram. A catch block that catches MyException can only catch a MyException i.e. any class that is-a MyException, which means MyException and any of its subclasses.

From the class diagram you can see that ArrayIndexOutOfBoundsException is not a MyException. ArrayIndexOutOfBoundsException certainly is-a Exception but not MyException.

Therefore, the catch block in main() will not be able to catch ArrayIndexOutOfBoundsException, which is what the explanation to option 1 says.
exception_hierarchy.png
exception_hierarchy.png (17.64 KiB) Viewed 6776 times

If there were another exception class SomeOtherImaginaryException that extended from MyException, and if doTest() method threw that exception, then catch(MyException) would have caught it.

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

apanah
Posts: 5
Joined: Sat Jun 21, 2014 3:49 pm
Contact:

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

Post by apanah »

Very clear - thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 87 guests