Here is a very simple code. You can further enhance for your learning.
Create exception ExceptionA
and define require constructors and methods:
public class ExceptionA extends Exception {
public ExceptionA(String message){
super(message);
}
}
Create exception ExceptionB
and define require constructors and methods:
public class ExceptionB extends ExceptionA {
public ExceptionB(String message){
super(message);
}
}
Create exception ExceptionC
and define require constructors and methods:
public class ExceptionC extends ExceptionB {
public ExceptionC(String message){
super(message);
}
}
Create TestException
class which catches ExceptionB
and ExceptionC
using ExceptionA
as below:
public class TestException {
public static void main(String[] args){
try{
getExceptionB();
}catch(ExceptionA ea){
ea.printStackTrace();
}
try{
getExceptionC();
}catch(ExceptionA ea){
ea.printStackTrace();
}
}
public static void getExceptionB() throws ExceptionB{
throw new ExceptionB("Exception B");
}
public static void getExceptionC() throws ExceptionC{
throw new ExceptionC("Exception C");
}
}