From JLS 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.
I thought the following code should compile. What is wrong here ?
public class Test{
{
// I get an error here: unreported exception TestException; must be caught or declared to //be thrown
print();
}
void print() throws TestException{
}
Test() throws TestException{
}
}
class TestException extends Exception{}