aspose file tools*
The moose likes Java in General and the fly likes Exception Handling In Instance Initializer Blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception Handling In Instance Initializer Blocks" Watch "Exception Handling In Instance Initializer Blocks" New topic
Author

Exception Handling In Instance Initializer Blocks

xing wu
Greenhorn

Joined: May 14, 2002
Posts: 9
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

Joined: Jan 13, 2002
Posts: 732
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

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 13, 2002
Posts: 732
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

Joined: Nov 07, 2000
Posts: 621
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

Joined: Apr 02, 2002
Posts: 3
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Arthur Wright
Greenhorn

Joined: Apr 02, 2002
Posts: 3
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
i have 1.4 and ilja one doesnt compile for me.
the TME one does.
welcome to the twilight zone!
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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

Joined: May 14, 2002
Posts: 9
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 13, 2002
Posts: 732
well done Ilja
one mystery solved!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jun 27, 2000
Posts: 213
Great! So both mysteries solved now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling In Instance Initializer Blocks