Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Problem about Java Specification3 8.2 Class Instance Initializer.

Thread view: 
skyofdreams - 30 Mar 2007 04:16 GMT
Hi,

I'm studying the following term of Java Spec3 8.2 Class instance
initializer.

=== quote ===
"It is compile-time error if an instance initializer of a named class
can throw a checked exception unless that exception or one of its
supertypes is explicitly declared in the throws clause of each
constructor of its class and the class has at least one explicitly
declared constructor. "
===

I wrote a small piece of code to test it.

===  code begin ==
package test.java.langspec.cls;

import java.io.*;
public class TestInstanceInitializer {

    {// instance initializer

        throw new IOException("aaa");
    }
   
    TestInstanceInitializer() throws Exception {
    }
}
===  code end ====

under Linux, Eclipse3.2,Jdk1.5_11, it report compile error
"Initializer does not complete normally".

I tried to wrap the throw statement into a member method, like this
===  code begin ==
package test.java.langspec.cls;

import java.io.*;
public class TestInstanceInitializer {

    {// instance initializer
        throwExcpt();
    }
   
    TestInstanceInitializer() throws Exception {
    }
   
    void throwExcpt() throws IOException {
        throw new IOException("aaa");
    }
}
===  code end ====
This time, it's OK,

but, my problem is why the first code pattern, it's doesn't work?
is any mystery under it ? ^_^

thanks in advance.
-skyofdreams
SadRed - 30 Mar 2007 05:45 GMT
> Hi,
>
[quoted text clipped - 56 lines]
> thanks in advance.
> -skyofdreams

Try this:
---------------------------------------
import java.io.*;
public class TestInstanceInitializer {

 {
   if (1 == 1){
     throw new IOException("aaa");
   }
 }

 TestInstanceInitializer() throws Exception {
 }
}
---------------------------------------
Method call and if statement are assumed to be 'can throw exception'
state as the spec describes whereas your first instance initialzer is
'always throws exception' state. If it always throws exception, it
can't complete normally.
skyofdreams - 30 Mar 2007 10:29 GMT
> Try this:
> ---------------------------------------
[quoted text clipped - 15 lines]
> 'always throws exception' state. If it always throws exception, it
> can't complete normally.

yes, it works.
Thanks SadRed

so I understand that i need continue to read the Exception chapter  of
JavaSpec3. [chuckle]

-Wisdo


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.