This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

que on instance initializer

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a doubt in the following stmt given in the JLS.
If a throw statement is contained in an instance initializer, then a compile-time check ensures that either its value is always an unchecked exception or its value is always caught by some try statement that contains it,
or the type of the thrown exception (or one of its superclasses) occurs in the throws clause of every constructor of the class.

I had written a program to understand the highlighted part of the aforementioned stmt. But i get a compile time error. Pl help me.


-Sanjana
[ January 02, 2004: Message edited by: sanjana narayanan ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the compiler error that you get?
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dirk,
The compiler error i get is instance initializer should be able to complete normally but then what is meant by the 'type of the thrown exception occurs in the throws clause of the every constructor'.
-sanjana
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW, I get the same error, using (Sun) JDK 1.4.2, on Windows 2000. There is an (old) bug in the Sun Java Bug Database about this very issue.. however, it's marked as "closed, fixed." OTOH, there are comments posted to the bug, indicating it was still present as late as JDK 1.3.0.
Off hand, I'd guess that the bug was never really fixed, or it's a regression.
Are you using the Sun JDK? If so, maybe try this with an IBM JDK and see if you get the same behavior, or behavior that matches the JLS... if the latter, it probably means that Sun's javac still has a problem.
Oh, yeah, if anybody wants to look at the bug, here's the URL:
http://developer.java.sun.com/developer/bugParade/bugs/4054256.html
[ January 04, 2004: Message edited by: Phil Rhodes ]
 
Phil Rhodes
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... upon further review, I wonder if maybe the JLS has changed it's language regarding that. A Google search for that error message turns up a few references from compiler vendors (GCJ, Borland) mentioning that it's illegal for static initalizers to throw exceptions, at all.
Time to dig out the language spec, huh?
Edit --- OK, make that "major edit"... I lost track of what we were talking about and went off on a tangent about static initializers, as opposed to instance initializers.
Anyway... Here's what the latest version of the JLS says:

8.6 Instance Initializers
An instance initializer declared in a class is executed when an instance of the class is created (�15.9), as specified in �8.8.5.1.
InstanceInitializer:
Block

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.

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.

It is a compile-time error if an instance initializer cannot complete normally (�14.20). If a return statement (�14.16) appears anywhere within an instance initializer, then a compile-time error occurs.

Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. See �8.3.2.3 for the precise rules governing forward reference to instance variables.
Instance initializers are permitted to refer to the current object this (�15.8.3) and to use the keyword super (�15.11.2, �15.12).

Given that, it looks like your code should compile... so maybe it is a bug in the compiler after all.
[ January 04, 2004: Message edited by: Phil Rhodes ]
 
Phil Rhodes
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, you have stumbled into one major dark corner of Java... :-)
Anyway, here's another bug report that talks about it some more, and a JDJ article that discusses these issues... I have to go get some sleep now, or I'd do some more poking around.. :-)
http://developer.java.sun.com/developer/bugParade/bugs/4926251.html
http://www.sys-con.com/story/?storyid=35532
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil Rodes,

That's great. Thank u so much for your time and elaborate explanation on the topic.
-Sanjana
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic