File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception handling in instance initializer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception handling in instance initializer" Watch "exception handling in instance initializer" New topic
Author

exception handling in instance initializer

Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
The following example is from Khalid book with little modification.

My question is if we comment out line3 like above, then we have a compiler error:
C:\WINDOWS\Desktop\pgjc-source\Example_8.6\ExceptionsInInstBlocks.java:14: initializer must be able to complete normally
{ // Instance initializer block
The compiler said "initializer must be able to complete normally". This is obviously not true( just compile with line 3 and see what happens). I just find it interesting the if(true) line make such difference when compiling.
Another question: Khalid said: "Instance initializer blocks in anonymous classe can throw any exceptions". But I have trouble compiling the following code (also from his book with little modification):

I found nowhere to declare the caught exception. The only way out is use try and catch block. Then the exception handling in instance initializer in this case will be the same as in a static intializer. Why Khalid said the former has greater freedom?


Sun Certified Java Programmer
Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
I think this is an interesting question and would welcome anybody to shed more light.
Tom
Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
I bring this up one more time. Though the posting is fairly long, but I assure you it's worth your time.
Rekha Rao
Greenhorn

Joined: Jan 23, 2001
Posts: 15
Just giving a shot at this one, not sure if it makes enough sense.
I think, you need to connect lot of things together here.
From JLS 8.6:
"It is a compile-time error if an instance initializer cannot complete normally "
From JLS: 14.20
"A break, continue, return, or throw statement cannot complete normally"

Hence the following code will give a compiler error ('initializer must be able to complete normally') .
class Test{
{
throw new TestException();
}
}
class TextException extends RuntimeException{
}

Also from JLS 14:20,
"HYPOTHETICAL: An if-then statement can complete normally iff at least one of the following is true:
The if-then statement is reachable and the condition expression is not a constant expression whose value is true.
The then-statement can complete normally
"
So, the following code compiles fine as, if-then statement is considered to complete normally (since if statement is reachable) and hence the instance initializer itself is considered to complete normally.
class Test{
{
if(true) // or if(false)
throw new TestException();
}
}
class TextException extends RuntimeException{
}
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
Declare the error in the constructor throws yourerror i think not sure.


Val SCJP <BR>going for SCJD
Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
Thanks Rekha. That is a very good try. Sometimes we have to go really deep into JLS to sort things out.
 
 
subject: exception handling in instance initializer