About Question enthuware.ocajp.i.v8.2.1385 :
Moderator: admin
-
- Posts: 5
- Joined: Mon Oct 03, 2016 3:41 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1385 :
A student submitted the following error and it was verified by a course mentor:
I've attached screenshots about a question I believe may be incorrectly written. In my understanding the method doSomethingElse() is throwing an unchecked expression therefor should be wrapped in either a try/catch or declared thrown in the signature or it will be a compiler error. The material however ignores this fact.
http://screencast.com/t/uSYprVQH
http://screencast.com/t/08xgVslaUB
I've attached screenshots about a question I believe may be incorrectly written. In my understanding the method doSomethingElse() is throwing an unchecked expression therefor should be wrapped in either a try/catch or declared thrown in the signature or it will be a compiler error. The material however ignores this fact.
http://screencast.com/t/uSYprVQH
http://screencast.com/t/08xgVslaUB
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
MyException class must extends RuntimeException.
I can't see this in your images.
I can't see this in your images.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
Your understanding is not correct. An unchecked exception need not be caught or be declared in the throws clause.corptrainer1 wrote: I've attached screenshots about a question I believe may be incorrectly written. In my understanding the method doSomethingElse() is throwing an unchecked expression therefor should be wrapped in either a try/catch or declared thrown in the signature or it will be a compiler error.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Jun 07, 2017 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
How can I modify the code to remove the ArrayIndexOutOfBoundsException and see MySpecialException in action?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
Instead of calling doSomething(); in main, you can call doSomethingElse();
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Jun 07, 2017 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
There is a CTE saying 'Cannot resolve MySpecialException.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
Well, you have to write it as well. The problem statement says, (Assume that MySpecialException is an unchecked exception.) Adding the following line will do -
class MySpecialException extends RuntimeException{ }
class MySpecialException extends RuntimeException{ }
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
I don't get why the exception is not caught anywhere.
So the question is:
What will the following code print when compiled and run?
(Assume that MySpecialException is an unchecked exception.)
In the explanation, at the end you state that "Since the exception is not caught anywhere, it will be thrown out to the JVM, which will print the stack trace of the exception."
Now, it is my understanding that unchecked exceptions such as an "ArrayIndexOutOfBoundsException" don't have to be caught or declared, BUT THEY CAN BE CAUGHT.
I believe that the unchecked exception is indeed being caught here:
So my understanding is that the way this should go is as follows:
At line 4, the method doSomething() is being called. Then at line 12 an ArrayIndexOutOfBoundsException (which is an unchecked exception) is being thrown. Then this exception is being caught at line 5.
If I type in the code, then it doesn't give the result I think it should, but I don't see where my logic fails.
Can you point out where my reasoning goes wrong?
Thank you!
So the question is:
What will the following code print when compiled and run?
(Assume that MySpecialException is an unchecked exception.)
Code: Select all
1. public class ExceptionTest {
2. public static void main(String[] args) {
3. try {
4. doSomething();
5. } catch (MySpecialException e) {
6. System.out.println(e);
7. }
8. }
9.
10. static void doSomething() {
11. int[] array = new int[4];
12. array[4] = 4;
13. doSomethingElse();
14. }
15.
16. static void doSomethingElse() {
17. throw new MySpecialException("Sorry, can't do something else");
18. }
}
Now, it is my understanding that unchecked exceptions such as an "ArrayIndexOutOfBoundsException" don't have to be caught or declared, BUT THEY CAN BE CAUGHT.
I believe that the unchecked exception is indeed being caught here:
So my understanding is that the way this should go is as follows:
At line 4, the method doSomething() is being called. Then at line 12 an ArrayIndexOutOfBoundsException (which is an unchecked exception) is being thrown. Then this exception is being caught at line 5.
If I type in the code, then it doesn't give the result I think it should, but I don't see where my logic fails.
Can you point out where my reasoning goes wrong?
Thank you!
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
Why do you think ArrayIndexOutOfBoundsException is being caught at line 5?
The catch at line 5 is for MySpecialException. Why do you think it will catch ArrayIndexOutOfBoundsException ?
HTH,
Paul.
The catch at line 5 is for MySpecialException. Why do you think it will catch ArrayIndexOutOfBoundsException ?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
Aha, I get it now!
I thought ArrayIndexOutOfBoundsException would be caught at line 5 because it was an unchecked exception, just like MySpecialException is an unchecked exception.
But it's not the same unchecked exception as ArrayIndexOutOfBoundsException, nor is it defined as its superclass. So therefore ArrayIndexOutOfBoundsException will not be caught.
Thanks for your response!
I thought ArrayIndexOutOfBoundsException would be caught at line 5 because it was an unchecked exception, just like MySpecialException is an unchecked exception.
But it's not the same unchecked exception as ArrayIndexOutOfBoundsException, nor is it defined as its superclass. So therefore ArrayIndexOutOfBoundsException will not be caught.
Thanks for your response!
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
It is not about whether the exception in the catch block is same or not. You need to see if the exception thrown in the try block satisfies the "is-a" test with respect to the exception class mentioned in the catch block.
For example, in this case, you need to see whether an instance of ArrayIndexOutOfBoundsException is-a MySpecialException? No. So an instance of ArrayIndexOutOfBoundsException cannot be caught by MySpecialException.
If the code in the try block threw an instance of a subclass of of MySpecialException, then it would have been caught by the given catch block.
HTH,
Paul.
For example, in this case, you need to see whether an instance of ArrayIndexOutOfBoundsException is-a MySpecialException? No. So an instance of ArrayIndexOutOfBoundsException cannot be caught by MySpecialException.
If the code in the try block threw an instance of a subclass of of MySpecialException, then it would have been caught by the given catch block.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1385 :
Thank you!
It is clear now.
It is clear now.
Who is online
Users browsing this forum: No registered users and 162 guests