• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in instance initializer !!!!!!!

 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
ref: JLS 8.6
"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. "
why is this code snippet giving compile-time error??
import java.sql.SQLException;
class test {
{
System.out.println("in init block");
throw new SQLException();
}
test() throws Exception{
System.out.println("in constructor");
}
public static void main(String[] args){
new test();
}
}

shanks.
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shankar, I can only answer part of your question.
Everything you have is throwing an exception, but nothing is catching one. You need to set up a try-catch around the statement in the main method to eliminate one of the compile-time errors.
I am interpreting JLS 8.6 the same as you are, I think, and I'm not sure why the second compile error is generated if 8.6 is correct. If you put the throw statement within the constructor rather than the initializer block, the code compiles, but 8.6 does appear to validate placing the throw statement within the initializer as well, as long as the constructor is declared to throw an Exception.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are doing new test(); in main method. Now, new test() throws an exception but main method does not have any throws clause.
If you read the compiler message carefully, if you'll understand what's going on.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i forgot to include the throws clause after main().
but that was not my question.
still u get a compile time error saying that the initializer should complete normally.but since i have specified the exception in the throws clause of the constructor, it should not give any errors according to JLS.
this is my question !!!
could anybone explain this ???
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see your point. I tried to compile it on jdk1.3.1 and on VAJ3.5. It does not compile on jdk1.3.1 but DOES work as expected on VAJ3.5.
Looks like IBM knows JLS better than Sun!
-Paul.
------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shankar,
You found a compiler bug See this earlier post on the same subject
http://www.javaranch.com/ubb/Forum24/HTML/007863.html
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul or Jane,
I tried to compile that code.
If I get the compiler complaining about not reaching the print statement after having the main method throwing the exception, what does this mean? Am I not using 1.3? I'm confused.
Ranjan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic