Question

JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This...

JAVA

Write a program that demonstrates how various exceptions are caught with

catch (Exception exception)

This time, define classes ExceptionA (which inherits from class Exception) and ExceptionB (which inherits from class ExceptionA). In your program, create try blocks that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and IOException. All exceptions should be caught with catch blocks specifying type Exception.

Homework Answers

Answer #1

import java.io.IOException;

//Exception A class
class ExceptionA extends Exception {
   private String msg;

   public ExceptionA(String aMsg) {
       super();
       msg = aMsg;
   }

   @Override
   public String toString() {
       return msg;
   }

}

// Exception B class
class ExceptionB extends ExceptionA {

   public ExceptionB(String aMsg) {
       super(aMsg);
   }

   @Override
   public String toString() {
       return super.toString();
   }

}

public class ExceptionTest {
   public static void main(String[] args) {
       try {
           // throwing ExceptionA and catching it
           throw new ExceptionA("ExceptionA example..!!!");
       } catch (ExceptionA e) {
           System.out.println(e);
       }
       try {
           // throwing ExceptionB and catching it
           throw new ExceptionB("ExceptionB example..!!!");
       } catch (ExceptionB e) {
           System.out.println(e);
       }
       try {
           // throwing NullPointerException and catching it
           throw new NullPointerException("NullPointerException example..!!!");
       } catch (NullPointerException e) {
           System.out.println(e);
       }
       try {
           // throwing IOException and catching it
           throw new IOException();
       } catch (IOException e) {
           System.out.println(e);
       }

   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Know the answer?
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
Similar Questions
In JAVA For practice using exceptions, this exercise will use a try/catch block to validate integer...
In JAVA 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...
PLEASE CODE IN C# NOT in Java EXCEPTION HANDLING Concept Summary: Use of try... catch in...
PLEASE CODE IN C# NOT in Java EXCEPTION HANDLING Concept Summary: Use of try... catch in a program Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try... catch and uses an existing exception in C#. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
Java Language: Write an application that asks the user to enter a student ID and a...
Java Language: Write an application that asks the user to enter a student ID and a test letter grade. Create an Exception class names GradeException that contains an Array of valid grade letters that you can use to determine whether a grade entered from the application is valid. In your application, throw a GradeException if the user does not enter a valid letter grade. Catch the GradeException, and then display an appropriate message. In addition, store an 'I' (for incomplete)...
Given the following program, which statements are true? public class ExceptionLabExercise {     public static void...
Given the following program, which statements are true? public class ExceptionLabExercise {     public static void main(String[] args) {         try {             System.out.println(args[0]);         } finally {             System.out.println("FINALLY");         }     } } C. Given the following classes, public class TestClass {     public static void main(String[] args) throws A {         try {             f();         } finally {             System.out.println("Done."); Test Stem / Question Choices 4: Run the class above. What happens when you run with...
*OBJECT ORIENTED PROGRAMMING* * JAVA PROGRAMMING* Create a program that simulates a race between several vehicles....
*OBJECT ORIENTED PROGRAMMING* * JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter must have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this will...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write an application from scratch where you are to write in Java the following methods to...
Write an application from scratch where you are to write in Java the following methods to be used by the main(): getFootValue() returning a Double value that is entered getInchValue() returning a Double value that is entered convertToCentimeters() returns a Double value using the calculated results from the getFootValue() and getInchValue() which will be used as the input values for the convertToCentimeters() method Next: The Exceptions that will be thrown if the values are invalid and these values are to...
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT