About Question enthuware.ocajp.i.v7.2.1326 :
Moderator: admin
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1326 :
What happened when you tried to compile and execute it?
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Thu Mar 19, 2020 2:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1326 :
as per book OCAJP by Hanuman deshmukh,page no-285, it says we cannot throw checked exceptions from static initializer but instance initializers are allowed to throw checked exceptions in this case we should declare an exception thrown from an instance initializer in the throws clause of each constructor of the class. please clarify
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1326 :
Not sure what you mean by clarify. The statement is quite clear itself. What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Thu Mar 19, 2020 2:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1326 :
what i mean is the explanation in the question says "If you try to throw a checked exception from a static or instance initializer block to the outside, the code will not compile." but the book says we can throw checked exceptions from the instance initializer if we declare it in all the constructors throws clause .since there is contraction between two statements i asked for an explanation .
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1326 :
The book is correct as shown by the following short code:
The explanation should be enhanced with this detail.
thank you for your feedback!
Code: Select all
public class TestClass{
{
if(true) throw new Exception(); //compiles
}
static {
if(true) throw new Exception(); //does not compile
}
TestClass() throws Exception{ }
}
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Tue Jan 11, 2022 4:41 am
Re: About Question enthuware.ocajp.i.v7.2.1326 :
And, if it is not the static block but a non-static block, the error is ArrayIndexOutOfBoundsException. So, does Oracle or others expect us to know that? How does it benefit anybody?
class Ax {
int [] x = new int[0];
{
x[0] = 1;
}
}
class Ax {
int [] x = new int[0];
{
x[0] = 1;
}
}
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1326 :
Hi Sijucm,
Our job is to prepare you for the exam. We we get into the argument of what is and what is not useful, we will not be doing that job well. But I understand your concern and rest assured that you are not alone. Many people have the same complaint. The right forum to raise your concern might be twitter or some other social media network.
Here, we try to keep the discussion focused on the task at hand, i.e., learning what we need to learn for passing the exam.
HTH,
Paul.
Our job is to prepare you for the exam. We we get into the argument of what is and what is not useful, we will not be doing that job well. But I understand your concern and rest assured that you are not alone. Many people have the same complaint. The right forum to raise your concern might be twitter or some other social media network.
Here, we try to keep the discussion focused on the task at hand, i.e., learning what we need to learn for passing the exam.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 43 guests