Question

(Catching Exceptions with Superclasses) Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

class ExceptionA extends Exception

{

public ExceptionA(String message)

{

   super(message);

}

public static void getExceptionA() throws ExceptionA

{

   throw new ExceptionA("Exception A");

}

}

class ExceptionB extends ExceptionA

{

public ExceptionB(String message)

{

   super(message);

}

public static void getExceptionB() throws ExceptionB

{

   throw new ExceptionB("Exception B");

}

}

class ExceptionC extends ExceptionB

{

public ExceptionC(String message)

{

   super(message);

}

public static void getExceptionC() throws ExceptionC

{

   throw new ExceptionC("Exception C");

}

}

class Main

{

public static void main(String[] args)

{

   try

   {

     getExceptionA();

   }

   catch(ExceptionA ea)

   {

     ea.printStackTrace();

   }

  

   try

   {

     getExceptionB();

   }

   catch(ExceptionA ea)

   {

     ea.printStackTrace();

   }

  

   try

   {

     getExceptionC();

   }

   catch(ExceptionA ea)

   {

     ea.printStackTrace();

   }

}

public static void getExceptionA() throws ExceptionA

{

   throw new ExceptionA("Exception A");

}

public static void getExceptionB() throws ExceptionB

{

   throw new ExceptionB("Exception B");

}

public static void getExceptionC() throws ExceptionC

{

   throw new ExceptionC("Exception C");

}

}

Please give thumbsup, if you like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
(Catching Exceptions with Superclasses) Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is wrong with this code? Please bold the changes that were made so this code...

    What is wrong with this code? Please bold the changes that were made so this code runs properly. The Problem is: 11.16 (Catching Exceptions with SuperClasses) Use inheritance to create an exception superclass (called Exception) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherites from ExceptionA and ExceptionC inherits from ExeptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC. I got this far but I'm still getting a lot...

  • THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly! THIS QUESTION WANTS THE A...

    THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly! THIS QUESTION WANTS THE A USER INPUT! SO FOR EXAMPLE THE CODE MUST MAKE IT SO THE USER HAS TO INPUT A TEMPERATURE. AND WHEN THE USER INPUTS A TEMP IT MUST OUTPUT IF THE TEMP ENTERED IS TO HIGH OR LOW. Again please read the question carefully and READ the notes I left also! Write a Thermostat class such that the user of the Thermostat...

  • Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException, PunctuationException, and CommaException. EndOfSentenceException is...

    Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException, PunctuationException, and CommaException. EndOfSentenceException is the parent class to PunctuationException, which is the parent class to CommaException. Test your classes in a Driver class with a main method that asks the user to input a sentence. If the sentence ends in anything except a period (.), exclamation point (!), or question mark (?), the program should throw a PunctuationException. If the sentence specifically ends in a comma, the program...

  • This assignment helps you understand Inheritance. We want to create a superclass or parent class called...

    This assignment helps you understand Inheritance. We want to create a superclass or parent class called Shape. This shape class will have just one method to setBorderColor. Now we need 3 subclasses that extend the Shape class, Circle, Square and Rectangle. In these classes we will need to compute the area of each of the shapes and print it to the console using System.out.println You will need to take in an additional parameter in each of these subclasses -- side...

  • There is a white space in between but nothing is missing it's just the way screenshot...

    There is a white space in between but nothing is missing it's just the way screenshot is taken Can someone help me to solve this question Get Homework Help C PART 1: (7 Marks) Cre PDAssignment 2 Q BOStart Assignment New tab X file:///C:/Users/ksimr/AppData/Local/Packages/Microsoft.MicrosoftEdge_8wekyb3d8bbwe/TempState/Downloads/Assignment%202%20Question%20 PART I: (7 Marks) Create an exception hierarchy of ExceptionA, ExceptionB and ExceptionC such that ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. 6 Write a test program to show that the catch block for...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • For practice using exceptions, this exercise will use a try/catch block to validate integer input from...

    For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...

  • JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance...

    JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance and polymorphism. In this assignment we will use inheritance and polymorphism to solve a problem. Part (a) of the figure below shows a symbolic representation of an electric circuit called an amplifier. The input to the amplifier is the voltage vi and the output is the voltage vo. The output of an amplifier is proportional to the input. The constant of proportionality is called...

  • Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and...

    Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and call methods Student will demonstrate the ability to pass values to and from methods Student will demonstrate the ability to catch exceptions Student will demonstrate the ability to create a text file Student will demonstrate the ability to validate input data Program Specifications: You to write a menu driven program. The program will use a switch statement. The cases will be as follows: Get...

  • Write a program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT