public class Math{ public class MyException extends Exception{}
public static void main(
String[] args){
MathObj m=new MathObj();
try{ m.op();}
catch(MyException e){ System.out.println("1");} }
public void op()throws MyException{
System.out.println("2");throw new MyException();}}
class MathObj extends Math{
public void op(){ System.out.println("3");}}
in the first m is object of Mathobj and reference of MathObj. to resolve the error
you need to remove the System.out.println("2");throw new MyException();
to System.out.println("2"); as the exception is already in try and catch block.
there is no need to throw it again. This is causing the error.
public class Math{
public class MyException extends Exception{}
public static void main(String[] args){
Math m=new MathObj(); try{ m.op();}
catch(MyException e){ System.out.println("1");} }
public void op()throws MyException{
System.out.println("2");throw new MyException();}}
class MathObj extends Math{
public void op(){ System.out.println("3");}}
here the m is object of Math but reference of MathObj. so it just executes the m.op in class mathobj .