// A public constructor throwing an exception.
public ConstructorTest(int j) throws Exception {
i = j;
System.out.println("Value of parameter is: " + i);
}
// Another public constructor throwing an exception.
public ConstructorTest(
String s) throws Exception {
st = s;
System.out.println("Value of parameter is: " + st);
}
public static void main(String args[]) {
try {
ConstructorTest c1 = new ConstructorTest(10);
ConstructorTest c2 = new ConstructorTest("Passed");
}
catch(Exception e) {}
}
}
Output from the program -
Value of parameter is: 10
Value of parameter is: Passed
hi friend , u have come to the right point ,
obviously constructors can throw exceptin at any time,
but now there is one problem with that, u have taken a simple example,...but if there is one super class , the sub class extending the super one,
now if i make an instance of a sub class ....the sub class constructor will call supers constructor implicitly,
so if the Super's constuctor throws an Exception ...it has to
catch in subclass constructor....so there must be try block in Sub's constructor....BUT....then try become before SUPER call statement...
which is not allowed ie SUPER must be first statement in Constructor .....so
FOR THIS problem there is no solution in
JAVA....
got it.........bye
dheeraj.....