This is my code block.
class Alpha{
public void Gamma() {
System.out.println("Alphas");
}
}
class Beta extends Alpha{
public void Gamma() throws Exception //Line 1
{
try {
System.out.println("Betas");
} catch(Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("xfg");
}
}
public static void main(String[] args) throws Exception {
Alpha g = new Beta();
g.Gamma();
}
}
This code fails to compile because I have added "throws" in Line1.
The compiler complains that overridden methods cannot throw exceptions.
Why so ?.
Why cant an overridden method throw an exception ?.
Because I can override a method from a base class by adding n lines of code in the child's class implementation.
And these added code can throw an exception so why I cant use "throws" in the overridden method ?.