Question: Question 3: public class ExceptionA extends Exception{ public ExceptionA(String exceptionMessage){ super(exceptionMessage); public void getExceptionA() throws ExceptionA; } } public class ExceptionB extends ExceptionA{ public ExceptionB(String
Question 3:
public class ExceptionA extends Exception{ public ExceptionA(String exceptionMessage){ super(exceptionMessage); public void getExceptionA() throws ExceptionA; }
} public class ExceptionB extends ExceptionA{ public ExceptionB(String exceptionMessage){ super(exceptionMessage); public void getExceptionB() throws ExceptionB{ throw new ExceptionB("An exception of type 'ExceptionB' has been thrown"); } } } public class ExceptionC extends ExceptionB{ public ExceptionC(String exceptionMessage){ super(exceptionMessage);
@Override public void getExceptionC() throws ExceptionC{ throw new ExceptionC("An exception of type 'ExceptionC' has been thrown"); } } } public class TestException{ public static void main(String[] args){ ExceptionB b=new ExceptionB(); ExceptionC c=new ExceptionC();
try{ b.getExceptionB(); } catch(ExceptionA e){ e.printStackTrace(System.err); } try{ c.getExceptionC(); } catch(ExceptionA e){ e.printStackTrace(System.err); } } }
My posted question:
Use the exception hierarchy from Question 3 to demonstrate that the order of catch block is important if you want to specifically catch every exception in the same hierarchy. You should define a method called someMethod( ) that can throw all three exceptions randomly. Call someMethod( ) in your test program and catch all three exceptions separately. Run your program a few times to make sure that someMethod( ) may throw different exception for different run.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
