Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling In Instance Initializer Blocks

 
xing wu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accdg. to one of my SCJP certification books " if an instance initializer block does not catch a checked exception that can occur during its execution, then the exception must be declared in the throws clause of every constructor in the class".
However, I tried coding it below and the compiler wont allow me to throw a checked exception :
class MyException extends Exception{
public MyException(String msg) { super(msg); }
}
public class MainClass {
public static void main(String args[]){
try {
new InitBlock();
}catch(MyException e){}
}
}
class InitBlock{
boolean test;
{
test = true;
if(test) throw new MyException("throw...");
}
InitBlock() throws MyException {
super();
}
}
Thanks for your help. This is my first posting
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi TME... you need to change your name to cpmply with the javaranch specification (first name last name and real).
as to your question, whats exactly seems to be the problem? the code you posted compiles correctly for me....
what jdk are u using?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried it and got the same problem, though it seems to work if you *call a method* that may throw an exception:

Strange...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roy Ben Ami:
as to your question, whats exactly seems to be the problem? the code you posted compiles correctly for me....
what jdk are u using?

For

I get the error message "Initializer does not complete normally". May be a bug of the Eclipse-compiler, though...
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja , your code doesnt compile in my computer too...

however TME code does.
weird... i guess you are right and only if we call it through a method it works.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make it official
"TME"
Thank you for your participation here at the Ranch. However, your name doesn't follow the JavaRanch guidelines. Please take a moment and change your "Publicly Displayed Name" by modifying your profile after reviewing the guidelines at http://www.javaranch.com/name.jsp
Thanks.
Sean
 
Arthur Wright
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static initializers are not allowed to throw any checked exceptions on pain of a compiler error. In fact, all the code in the static initializer must be reachable, otherwise you'll see a similar error. See the language spec. section 8.7 for more.
Ciao
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arthur - he's not talking about a static initialiser. It's something I didn't even know existed - an instance initialiser block.
Don't really know for what you'd want to do that. Sharing initialisations between several constructors is presumably the idea. But you can do that by calling this(args) from one constructor, to call another.
 
Arthur Wright
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter et al:
Sorry about the static. JLS 8.6 talks about instance initializers. Constructors must throw any checked exceptions thrown by instance initializers. Otherwise, the rule is the same: The initializer may not contain unreachable code.
Gr�ss,
Arthur
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange - I can compile both TME's original code, and Ilja's code, using either SDK 1.3.1 or 1.4.0. What SDKs are you guys using? There have been bugs for similar code in the past though - seeBug 4054256. This is rougly the same as what you're discussing, except it occurs in an inner class. The code here fails under SDK 1.3.1 and before, but works for 1.4.0. For me anyway.
[ May 15, 2002: Message edited by: Jim Yingst ]
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 1.4 and ilja one doesnt compile for me.
the TME one does.
welcome to the twilight zone!
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both the code snippets above don't compile using Sun's jdk1.3.
I was going through the JLS and found some ambiguity (or atleast that's what I thought):
§11.2
"Static initializers (�8.7), class variable initializers, and instance initializers or instance variable initializers within named classes and interfaces (�8.3.2), must not result in a checked exception; if one does, a compile-time error occurs. No such restriction applies to instance initializers or instance variable initializers within anonymous classes (�15.9.5)."
§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. An instance initializer in an anonymous class (�15.9.5) can throw any exceptions. "
"It is a compile-time error if an instance initializer cannot complete normally (�14.20). "
[ May 15, 2002: Message edited by: Junaid Bhatra ]
 
xing wu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just updated my display name.
I'm still using SDK1.2 compiler though. I'll try the higher versions. Thanks for your comments.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Junaid Bhatra:
"It is a compile-time error if an instance initializer cannot complete normally (�14.20). "

That's the point I missed. It seems that there has to be a (potential) "normal" execution path in the initializer, so

*does* compile correctly... :roll:
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well done Ilja
one mystery solved!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. As for another (minor) mystery - earlier when I said I could compile Ilja's code, I meant the first code he posted (which he already said he could compile). In my haste I didn't register that there were two different posts from Ilja next to one another.
As for the JLS quotes - interesting. I had already seen section 8.6, and it does account for all the behavior we see here. However I hadn't noted section 11.2, which seems to contradict it. Since the two sections are incompatible, and 8.2 is both more detailed, and accurately describes the behavior of modern Java compilers, I'd regard 11.2 as just plain wrong. Good catch, Junaid. I'll drop Sun a line about that.
[ May 16, 2002: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Gilad Bracha:
Yes, this is a known bug in 11.2.
Even though we know about it, it's good to get these reports. Thanks.

Cool. Now if they only had an errata...
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! So both mysteries solved now
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic