| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
|
Advertisement
|
Why am I getting unreported exception when the super class default constructor has a 'throws' clause?
class Super {
public Super() throws Exception {
System.out.println("Super Class");
}
}
public class Sub extends Super {
public static void main(String[] args) throws Exception {
Sub s = new Sub();
}
}
Compile it and you have compile-time error:
Sub.java:6: unreported exception java.lang.Exception in default constructor
public class Sub extends Super {
1 error
Here is a Sun's Bug Report which can answer this question:
To fix it, you have to explicit to define a default constructor with an appropriate throws clause:
class Super {
public Super() throws Exception {
System.out.println("Super Class");
}
}
public class Sub extends Super {
public Sub() throws Exception {
}
public static void main(String[] args) throws Exception {
Sub s = new Sub();
}
}
Further Reading8.8.9 Default Constructor In the Java Language Specification 3rd Edition |