Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Wed Apr 30, 2014 1:41 pm
by lecadre24
It must declare throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException...
If I understand the explanation declaring RuntimeExceptions is optional and not a must.
So why is "It must ..." correct?
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Wed Apr 30, 2014 8:27 pm
by admin
This option is not marked as correct.
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Mon Jul 28, 2014 2:08 am
by pfilaretov
"It must declare throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException" still marked as correct one.
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Mon Jul 28, 2014 2:19 am
by pfilaretov
pfilaretov wrote:"It must declare throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException" still marked as correct one.
sorry, that's not true. Just looked at checkbox, not at correct answers
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Sun Oct 26, 2014 6:14 pm
by GurmeetGulati
Can you please elaborate on your explanation and answers.
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Sun Oct 26, 2014 7:33 pm
by admin
Sure, if you can tell which part do you have a problem understanding, I can explain further.
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Thu Apr 16, 2015 8:29 am
by rafaparche
Hi, I don't understand why It is ok if it declares just throws ArrayIndexOutOfBoundsException and It does not need to declare any throws clause are valid answers?
Thanks!
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Thu Apr 16, 2015 9:26 am
by admin
Since ArrayIndexOutOfBoundsException is a RuntimeException, it can be specified in any throws clause without any issue. Also, since it is a RuntimeException, there is no need to specify it in any throws clause either.
I suggest you to write some code to try out a few scenarios involving this.
HTH,
Paul
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Mon Apr 10, 2017 2:59 am
by Chandu
Consider the following hierarchy of Exception classes :
java.lang.RuntimeException
+-------- IndexOutOfBoundsException
+---------ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException
Which of the following statements are correct for a method that can throw ArrayIndexOutOfBounds as well as StringIndexOutOfBounds Exceptions but does not have try catch blocks to catch the same?
"The method calling this method will either have to catch these 2 exceptions or declare them in its throws clause"
This option is not valid in this question is because it is case of Runtime exception, but it would have been correct if it was case of Compile time exception right?
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Mon Apr 10, 2017 5:47 am
by admin
Yes, it would have been correct if they were checked exceptions. The method could also either catch a super class of these two exceptions (such as Exception or Throwable) or declare in its throws clause.
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Wed Jan 01, 2020 2:05 pm
by girishankaran
Hi All,
Why the following is wrong?
It must declare throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException
and how this is right?
It is ok if it declares just throws ArrayIndexOutOfBoundsException
Thanks
Re: About Question enthuware.ocajp.i.v7.2.1223 :
Posted: Wed Jan 01, 2020 7:39 pm
by admin
Already explained above. It is invalid because of "must".
Since both are RuntimeExceptions, there is no need to specify them in any throws clause.