Shilpa,
But then how can you actually catch the exception. look at my version of code.
class MyException extends Exception
{
}
class Test{
Test() throws MyException
{}
Test(int a){}
}
public class TechnoSample extends Test
{
TechnoSample()
{
}
public static void main(String args[])
{
System.out.println("hello world");
try{
TechnoSample t = new TechnoSample();
}
catch(MyException m){}
catch(Exception e){}
}
}
Here parent class Test has a contructor which throws a checked exception. Now the child class TechnoSample constructor does not throw it. I tried it and compiler yelled, exception must be caught.
You know this is because of in
TechnoSample(){
// implicit call to super();
}
now this super() call has something to do with exception. if try it manually
like,
TechnoSample(){
try{super();}catch(Exception e){}
}
But still Mr. Compiler is not very happy. he wants that super() must be the first line of code.
So the only way to make this code compiled, is to make child class constructor throws exception as well.