Create an exception hierarchy of ExceptionA, ExceptionB and ExceptionC such that ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a test program to show that the catch block for supertype exception (ExceptionA) can catch all the subtype ex- ceptions (ExceptionB and ExceptionC). Print out a message such as "ExceptionB caught" to indicate which type of exception is caught. Name your class file as ExceptionHierarchy.java.

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter11: Exception Handling
Section: Chapter Questions
Problem 2CP
icon
Related questions
Question

this is a java program code please do the code what in the question says and please follow what in the question says and dont copy or plagarise from other sources 

PART I:
Create an exception hierarchy of ExceptionA, ExceptionB and ExceptionC such that ExceptionB
inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a test program
to show that the catch block for supertype exception (ExceptionA) can catch all the subtype ex-
ceptions (ExceptionB and ExceptionC). Print out a message such as "ExceptionB caught" to
indicate which type of exception is caught. Name your class file as ExceptionHierarchy.java.
Hints:
1. To build the exception hierarchy, ExceptionA, ExceptionB and ExceptionC may have
empty body.
2. You may use System.err.println("ExceptionB caught"); instead of
System.out.println("ExceptionB caught"); so that the message will be output in
red color and in the format of exception messages. This is a more common way to output error
messages.
PART II:
Use the exception hierarchy from PART I to demonstrate that the order of catch block is important
if you want to specifically catch every exception in the same hierarchy. In other words, now you
should be able to catch subtype exceptions (ExceptionB and ExceptionC) separately rather
than using one catch block of supertype exception (ExceptionA) as you did in PART I.
You should define a method called some Method () that can throw all three exceptions randomly.
To do this, generate a random integer of 1, 2 or 3 representing ExceptionA, ExceptionB or
ExceptionC respectively, and then throw the corresponding exception. Output the random number
in some Method () to show which type of exception is randomly generated. Invoke some Method ()
in your test program and catch all three exceptions separately in three catch blocks. In each catch
block, print out a message such as "ExceptionA caught" to indicate which type of exception is
caught. Run your program a few times to make sure that some Method () may throw different
exception for different run and you are able to catch all three types of exceptions. Name your class
file as CatchDifferentExceptions. java.
Transcribed Image Text:PART I: Create an exception hierarchy of ExceptionA, ExceptionB and ExceptionC such that ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a test program to show that the catch block for supertype exception (ExceptionA) can catch all the subtype ex- ceptions (ExceptionB and ExceptionC). Print out a message such as "ExceptionB caught" to indicate which type of exception is caught. Name your class file as ExceptionHierarchy.java. Hints: 1. To build the exception hierarchy, ExceptionA, ExceptionB and ExceptionC may have empty body. 2. You may use System.err.println("ExceptionB caught"); instead of System.out.println("ExceptionB caught"); so that the message will be output in red color and in the format of exception messages. This is a more common way to output error messages. PART II: Use the exception hierarchy from PART I to demonstrate that the order of catch block is important if you want to specifically catch every exception in the same hierarchy. In other words, now you should be able to catch subtype exceptions (ExceptionB and ExceptionC) separately rather than using one catch block of supertype exception (ExceptionA) as you did in PART I. You should define a method called some Method () that can throw all three exceptions randomly. To do this, generate a random integer of 1, 2 or 3 representing ExceptionA, ExceptionB or ExceptionC respectively, and then throw the corresponding exception. Output the random number in some Method () to show which type of exception is randomly generated. Invoke some Method () in your test program and catch all three exceptions separately in three catch blocks. In each catch block, print out a message such as "ExceptionA caught" to indicate which type of exception is caught. Run your program a few times to make sure that some Method () may throw different exception for different run and you are able to catch all three types of exceptions. Name your class file as CatchDifferentExceptions. java.
Expert Solution
steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Exception Handling Keywords
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning