aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializer doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializer doubt" Watch "Initializer doubt" New topic
Author

Initializer doubt

megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
From JLS


An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses 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.




According to it Why does the following code not compile?Please guide...



Thanks,
Megha
[ May 18, 2007: Message edited by: megha joshi ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Megha,

What I see here, static initializer or instance initializer can't complete
abnormally. As you have faced this sort of scenario before, I remember.
You have to place the code inside the try catch block, and there is nothing
like throws clause in these cases. And yeah, if there is some exception
thrown at run time, the JVM will wrap that exception in the
ExceptionInInitializationError and rethrow it. For example it instance
or static initializer throws ArrayIndexOutOfBoundsException (you try it),
JVM wraps it in the ExceptionInInitializationError and rethrows it.






Thanks,
[ May 18, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi Chandra,

I know the scenario you said...but I was trying to code something to prove the following

From JLS


quote:
--------------------------------------------------------------------------------

An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses 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.


Let me know if you can shed some light on this statement with some example.

Thanks,
Megha
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Megha,

I got the reasoning behind what you say:

An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses 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 can throw any exceptions.

The rules above distinguish between instance initializers in named and anonymous classes. This distinction is deliberate. A given anonymous class is only instantiated at a single point in a program. It is therefore possible to directly propagate information about what exceptions might be raised by an anonymous class instance initializer to the surrounding expression. Named classes, on the other hand, can be instantiated in many places. Therefore the only way to propagate information about what exceptions might be raised by an instance initializer of a named class is through the throws clauses of its constructors. It follows that a more liberal rule can be used in the case of anonymous classes. Similar comments apply to instance variable initializers. [JLS 8.6 Instance Initializers]



I find it buggy, I tried this as per the JLS says:





But one bizarre thing I see on my NetBeans IDE, if don't declare IOException
of superclass of it in the constructor, Line 2 and Line 3 show error.
Otherwise it only says, Instance initializer completes abruptly.
I am trying to get this issue. Let you know


Thanks,
[ May 18, 2007: Message edited by: Chandra Bhatt ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Got it Megha,

See the code below:



What I found: Instance initializer can't complete abruptly, compiler checks
whether exception is recognized at compile time, what was happening in case
you write throw without any condition. So it is known fact at compile time
hence compiler error.

I forgot to read, the next couple of lines from the JLS after what you
posted. It explicitly states about this fact that at compile time
the instance initializer must complete without any abrupt ion otherwise
compiler error.

If we modify the code:





Thanks,
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Thanks Chandra...

So it confirms that the compiler will always check that any instance initializer or static intializer does not throw exception during its flow analysis.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Exactly Megha!



Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Initializer doubt