About Question enthuware.ocajp.i.v7.2.826 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.826 :
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.
In option A, if I am not wrong, a track trace is printed, isn't?
Thanks in advance.
-
- Posts: 19
- Joined: Wed Aug 01, 2012 3:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
Code: Select all
int[] array = new int[10];
array[10] = 1000;
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
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.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.
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.
Regards,
Javanaut
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
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.
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.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
Hi Paul,
Ah, yes this explanation makes sense. Thank-you for explaining this. That is tricky.
Regards,
Javanaut
Ah, yes this explanation makes sense. Thank-you for explaining this. That is tricky.
Regards,
Javanaut
-
- Posts: 2
- Joined: Tue Jan 07, 2014 1:57 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
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.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.
thanks
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
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.
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.
-
- Posts: 2
- Joined: Tue Jan 07, 2014 1:57 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
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 ?
-
- Posts: 5
- Joined: Sat Jun 21, 2014 3:49 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
I had the same thought - can you respond on this?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 ?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
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.
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.
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.
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.
-
- Posts: 5
- Joined: Sat Jun 21, 2014 3:49 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.826 :
Very clear - thanks!
Who is online
Users browsing this forum: No registered users and 87 guests