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
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