Problem about Java Specification3 8.2 Class Instance Initializer.

S

skyofdreams

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
 
S

SadRed

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

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

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

TestInstanceInitializer() throws Exception {
}
}
 
S

skyofdreams

SadRed said:
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.

yes, it works.
Thanks SadRed

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

-Wisdo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top