wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in Creating the Instance Intializer Block against JLS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in Creating the Instance Intializer Block against JLS" Watch "Problem in Creating the Instance Intializer Block against JLS" New topic
Author

Problem in Creating the Instance Intializer Block against JLS

sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
Hi All,

I am refering the JLS (Instance Intializers Chapter 8.6) which tells the following thing

An instance initializer declared in a class is executed when an instance of the class
is created (§15.9), as specified in §8.8.7.1.
InstanceInitializer:
Block

It is compile-time error if an instance initializer of a named class can throw a
checked exception unless that exception or one of its supertypes is explicitly
declared in the throws clause of each constructor of its class and the class has at
least one explicitly declared constructor.
An instance initializer in an anonymous
class (§15.9.5) can throw any exceptions.


And i tried to create a code as the per the JLS, i not able to suceed.



i am getting compile time error "Initializer does not complete normally" in line 1, but as per the above quoted JLS Stanza, it should not give compile time error because
1) I have atleast one default constructor
2) that Constructor is also throwing the same Checked, IOException, what i am throwing in intance intializer block

So can someone tell me where i am going wrong here.


Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
sakthi moorthy wrote:It is compile-time error if an instance initializer of a named class can throw a
checked exception unless that exception or one of its supertypes is explicitly
declared in the throws clause of each constructor of its class

The important word in that quote is 'can'. There has to be a possibility that an exception can't be thrown, otherwise an instance of the class could never be created in which case there is no point to the class.
Try this
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
Hi Stuart,


Thanks for the reply. I will tell my understanding of the quoted JLS Sentence.
It is compile-time error if an instance initializer of a named class can throw a
checked exception unless that exception or one of its supertypes is explicitly
declared in the throws clause of each constructor of its class and the class has at
least one explicitly declared constructor


In order to throw a checked exception from Instance Intializer following conditions needs to be statisfied

1) At Least One explicitly declared Constructor Say HHH() in this case has to be present
2) The Same that is HHH() Constructor has to throws A Exception or the Exceptions Super Type, so declared the constructor like following



If above conditions are satisfied, you can have an instance intializer block that can throw a Checked Exception like following


I am not able to relate my understanding with your code, to be specific i am not able to understand why you have kept if block in instance intializer. If i am not wrong, statements under conditional checking block will be executed only on certain condition or conditions, which may or may not happen,thats why that compilation problem has resolved.





Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
In your code an IOException will always be thrown, therefore it is impossible to create an instance of your class. Your code is therefore basically useless. The compiler realises this and so doesn't let you do it. In my code there's a path through the initialiser that will not throw an exception (when s is false) and so the compiler does not complain.

See section 8.6 of the Java 7 JLS
It is a compile-time error if an instance initializer cannot complete normally
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
Thanks Stuart i got it now


I wanted to convey one more thing here the code


will compile fine until the default Constructor HHH() throws IOException or IOException's super type, which means if you want to throw a Exception in Intializer block, the same Exception should be thrown in all Constructors thats what the JLS Says. so the following code wont compile.






Again thanks Stuart
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Creating the Instance Intializer Block against JLS
 
Similar Threads
Exception and Instance Initializer
Checked Exception in instance intializer !!!
Initializer doubt
Exception in instance initializer !!!!!!!
Exception Handling In Instance Initializer Blocks