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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

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

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

Post by admin »

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.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

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

Post by swarna pakeer »

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

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

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

Post by admin »

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.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

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

Post by swarna pakeer »

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 .

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

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

Post by admin »

The book is correct as shown by the following short code:

Code: Select all

public class TestClass{

  {
    if(true) throw new Exception(); //compiles
  }

  static {
    if(true) throw new Exception(); //does not compile
  }

  TestClass() throws Exception{ }
}
The explanation should be enhanced with this detail.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

sijucm
Posts: 8
Joined: Tue Jan 11, 2022 4:41 am

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

Post by sijucm »

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;
}
}

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

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

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests