• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Instance initializers in anonymous class

 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Can Instance intializers in anonymous classes throw any exception ?
well .. , am nt getting hold of this point , at page 263 , in Khalid Mughal book , it is stated as:


...Instance initializer blocks in anonymous classes have greater freedom : they can throw any exception ....


Sec 8.6 of JLS 2nd edition also second the above statement
wht does above statement exactly means?
Leave da talk of "any exception" , compiler is not allowing to throw even a unchecked exception , as in following code

the compiler complains this way :


--------------------------- Compiler Output ---------------------------
AnonymousCheck.java:23: initializer must be able to complete normally
{
^
1 error



I guess , i am interpreting the statement in some-way it is not intended to be interpreted
Can anybody plz help me understand this thing ..i am confused now


------------------
Gagan (/^_^\)
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler detects that your code:
a=10;
b=15;
throw new RuntimeException(); //<--- here i throw

Will ALWAYS throw an exception and thus never complete the initializer.
Bill
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankx William for ur quick reply !
i changed the code as per ur suggestion :


and now the compiler accepts the above code
But if i try to throw any checked exception , then complier wants me to handle it in the instance initializer only
like if i change the part from above code to
......
a=10;
b=15;
if(true)
throw new Exception(); // <-- checked Exception here
........
then compiler complains like :


--------------------------- Compiler Output ---------------------------
AnonymousCheck.java:27: unreported exception java.lang.Exception; must be caught or declared to be thrown
throw new Exception(); //<--- checked Exception here
^
1 error



so does this means tht code in instance initalizer can not throw checked exceptions without handling them in the block itself ?
or if i choose to "pass it" , or as compiler suggests "declare it to be thrown" , then where exactly i do tht? becoz da syantax of anonymous classes doesnt support any "throws" clause
( and obviously , we don hav ne constructors )
so wht does " instance initializers can throw any exception " thing really means ?
plz help , i am still confused

------------------
Gagan (/^_^\)
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What an interesting question - I don't know how you could declare that a static initializer throws an exception. The JLS says that if the initializer throws an exception that is not a Error or subclass of Error, the JVM is supposed to create a new exception of the ExceptionInInitializerError type based on the real exception but it doesn't say how you declare an Exception.
Maybe you could try putting throws Exception in various places and see what happens....
Bill
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an answer I found.
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bug #4409174: http://developer.java.sun.com/developer/bugParade/bugs/4409174.html
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have read sometime back that, there are certain things which the JLS says should happen, but the javac doesnot allow to do so, i.e. JDK 1.2 or JDK 1.3 is not 100% JLS compiler.
The JLS says that if the instance initializers throw checked exceptions then every constructor shoud declare that exception in the throws clause.However, the Sun's JDK does not allow this to compile!
There is a compiler by IBM called Jikes which is 100% JLS compliant.Try to compile the code using that compiler and see if you are able to compile it.
- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankx William , Marilyn , Mapraputa & Sandeep for ur help
well..i now knw , it is indeed a fact, Javac DO NOT allow certain things which JLS 'll happily accept!
more on this can be found at URL provided by Marilyn , where Roseanne maintains a nice FAQ http://members.spree.com/education/javachina/Cert/FAQ_SCJP7.htm#som_Q2
I hav downloaded JIKES ( a open source java compiler from IBM , http://www-124.ibm.com/developerworks/opensource/jikes/ )
and well , this compiler seem to agree with JLS on some topics , more than Javac
( n i recommend u to download n c Jikes at work , it is less than 1mb download , and it is really really fast , if u just hav to c whether a code compiles or nt , it is da fastest way to check )

as suggested by Mapraputa , there are few bugs in Javac , on the topic of throwing exceptions in instance initializers
but the bug #4409174 is about "instance initializer in NAMED classes "
( and this bug is not present in JIKES
as following code is acceptable by JLS 2nd ed ,
Javac of JDK1.3 wont compile it , but Jikes does

)
But my problem is about , anonymous classes
There are few bugs listed in bug database , which are close to my problem
( i havnt found any exact match , plz letme knw if anyone found exact )
( some related bugs are #4350863,#4266172,#4063948 )
A related bug #4207953 (Anonymous class' superclass' constructor throwing exception gives compiler error) , lists follwoing code :

the above bug is fixed , n code is acceptable by Javac (jdk1.3) , but nt by Jikes ( strange , i thought Jikes to be fully JLS compatible?! )

Now i experimented with my code alot , trying to place "throw" n "throws" at many apparent and not-so-apparent places , n i now think following code should be acceptable according to JLS 2nd ed


so how many of u out there think tht above code should be acceptable according to JLS ?
all i am trying to do is to throw a checked exception from a instance initializer of anonymous class ,
whose super class's only constructor neatly declares to throw this checked-exception
( so the implicit constructor for anonymous class formed by compiler will include this "throws" declaration , as per my understanding from JLS �15.9.5 )
or am i interpreting the things in my way ?
( well , neither do Javac(jdk1.3)[1 error] , nor do Jikes[2 errors] accepts the above code tho )
ANY type of comments , suggestion , links on the above topic are more than welcome ,
things getting lill messy about this , so plz everybdy out there come out with anything u hav to say , anything
i except we can discuss it to reach some concrete conclusion

------------------
Gagan (/^_^\)
 
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gagan,
Ur last code works well as u have already correctly mentioned the reason, i.e.
throwing checked exceptions in instance initializer code blocks require....(1)catching in that code block or (2)catching it in all the constructors or (3)declare them in the constructors' headers as throws....
------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer For Java 2 Platform
--When you learn something, learn it by heart!
[This message has been edited by Ashik uzzaman (edited September 02, 2001).]
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashik , u mean the last code by me SHOULD work well , or does the code really works on ur side??
well on my side , Javac of JDK1.3 gives following error ;


--------------------------- Compiler Output ---------------------------
SimpleTest.java:12: unreported exception java.lang.Throwable; must be caught or declared to be thrown
SimpleTest test = new SimpleTest() { { if(true) throw new Throwable(); } };
^
1 error



And Jikes 1.14 gives 2 errors


Found 2 semantic errors compiling "C:/My Documents/JavaCode/SimpleTest.java":
12. SimpleTest test = new SimpleTest() { { if(true)
throw new Throwable(); } };
<-----------------------------<br /> -------------------------->
*** Error: A constructor associated with this anonymous type does not throw the
exception "java/lang/Throwable" thrown by its super type, "SimpleTest".

12. SimpleTest test = new SimpleTest() { { if(true)
throw new Throwable(); } };
<-------------------->
*** Error: The type of the expression in this throw statement, "java/lang/Throwa
ble", is not a subclass of RuntimeException or Error.



------------------
Gagan (/^_^\)
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Gagan,
It gives the error as u mentioned above...i was compiling after indenting ur code where i mistakenly gave a ; in ur code pointed by red color ...and the code was not working with anonymous class and only a local code block of main method which compiled and ran successfully throwing a runtime exception....
SimpleTest test = new SimpleTest(); { { if(true) throw new Throwable(); } };
So ur problem still persists showing that JDK1.3 is not 100% JLS compiler....
------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer For Java 2 Platform
--When you learn something, learn it by heart!
[This message has been edited by Ashik uzzaman (edited September 03, 2001).]
 
I've never won anything before. Not even a tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!