Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi Everybody, I need the answer for this question as soon as possible because I

ID: 670147 • Letter: H

Question

Hi Everybody,

I need the answer for this question as soon as possible because I need to submit it tomorrow night

(Catching Exceptions with Superclasses) Use inheritance to create an exception superclass

(called ExceptionA) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherits

from ExceptionA and ExceptionC inherits from ExceptionB. Write a program to demonstrate

that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC.

Suggested Solution

The following provides a suggestions for completing this assignment, Problem 11.16

1.  Create the "ExceptionA", "ExceptionB", and "ExceptionC".  "ExceptionA" should inherit from the appropriate class that allows this exception to be thrown. "ExceptionB" extends from "ExceptionA". "ExceptionC" extends from "ExceptionB".

2.  Create a class "ExceptionTester" that will contain a main method.

3.  The main() method should perform the following:

Create 1 object of each exception type (ExceptionA, ExceptionB, and ExceptionC)

Within a separate try block throw each exception type, passing a message (refer to "main" method pg. 451 for an example of a single try catch block)

Within each associated try blocks create a catch block that catches an "ExceptionA" object

  

Because of Inheritance an "ExceptionA" handler will catch "ExceptionA", "ExceptionB" and "ExceptionC" objects.

All this program should demonstrate is that if any of the above exception objects is thrown, the "ExceptionA" handler will catch.

Explanation / Answer

Here is a very simple code.

Create exception ExceptionA and define require constructors and methods:

Create exception ExceptionB and define require constructors and methods:

Create exception ExceptionC and define require constructors and methods:

Create ExceptionTester class which catches ExceptionB and ExceptionC using ExceptionA as below: