This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception in instance initializer !!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception in instance initializer !!!!!!!" Watch "Exception in instance initializer !!!!!!!" New topic
Author

Exception in instance initializer !!!!!!!

shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
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

Joined: May 07, 2001
Posts: 195
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

Joined: Sep 23, 2000
Posts: 3335
    
    8
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
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

Joined: Sep 23, 2000
Posts: 3335
    
    8
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ranjan Chaudhuri
Ranch Hand

Joined: May 22, 2001
Posts: 33
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in instance initializer !!!!!!!