0% found this document useful (1 vote)
9 views2 pages

Java Exception Inheritance Example

The document provides guidance on how to create an exception hierarchy with a superclass and subclasses to demonstrate that a catch block for the superclass will catch exceptions of the subclasses. It recommends: 1. Creating exception classes ExceptionA, ExceptionB, and ExceptionC where ExceptionB extends ExceptionA and ExceptionC extends ExceptionB. 2. Creating a class ExceptionTester with a main method to throw exceptions of each type and catch using an ExceptionA handler. 3. Within try blocks in the main method, throw instances of ExceptionC then ExceptionB. Catch blocks using ExceptionA will catch both subclasses and output messages.

Uploaded by

sandeepsai90
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
9 views2 pages

Java Exception Inheritance Example

The document provides guidance on how to create an exception hierarchy with a superclass and subclasses to demonstrate that a catch block for the superclass will catch exceptions of the subclasses. It recommends: 1. Creating exception classes ExceptionA, ExceptionB, and ExceptionC where ExceptionB extends ExceptionA and ExceptionC extends ExceptionB. 2. Creating a class ExceptionTester with a main method to throw exceptions of each type and catch using an ExceptionA handler. 3. Within try blocks in the main method, throw instances of ExceptionC then ExceptionB. Catch blocks using ExceptionA will catch both subclasses and output messages.

Uploaded by

sandeepsai90
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

(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.

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


ExceptionB, and ExceptionC)
Within a separate try block throw
a message (refer to "main" method
single try catch block)
Within each associated try blocks
catches an "ExceptionA" object

type (ExceptionA,
each exception type, passing
pg. 451 for an example of a
create a catch block that

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.

public class Demo


6{
7 public static void main( String args[] )
8{
9 try // throw ExceptionC
10 {
11 throw new ExceptionC();
12 } // end try
13 catch ( ExceptionA exception1 ) // catch ExceptionA and subclasses
14 {
15 [Link]( "First Exception subclass caught. \n" );
16 } // end catch
17
18 try // throw ExceptionB
19 {
20 throw new ExceptionB();
21 } // end try
22 catch ( ExceptionA exception2 ) // catch ExceptionA and subclasses
23 {
24 [Link]( "Second Exception subclass caught. \n" );
25 } // end try
26 } // end main
27 } // end
class ExceptionA extends Exception
31 {
32 // empty class body
33 } // end class ExceptionA
34
35 class ExceptionB extends ExceptionA
36 {
37 // empty class body
38 } // end class ExceptionB
39
40 class ExceptionC extends ExceptionB
{
42 // empty class body
43 }

You might also like