Question

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 of errors, please bold the differences and explain what the problems(s) were.

@SuppressWarnings("serial")

/**
*
* @author Lauren
*/
public class Exercise1116A extends Exercise1116 {
public Exercise1116A(String message){
super(message);
}
}

@SuppressWarnings("serial")

/**
*
* @author Lauren
*/
class Exercise1116B extends Exercise1116A{
public Exercise1116B(String message){
super(message);
}
}

@SuppressWarnings("serial")

/**
*
* @author Lauren
*/
class Exercise1116C extends Exercise1116B {
public Exercise1116C(String message){
super(message);
}
}

public class Exercise1116 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{getExercise1116B ();}
catch (Exercise1116A ea)
{ea.printStackTrace();}
try
{getExercise1116C ();}
catch (Exercise1116A ea)
{ea.printStackTrace();}
}
public static void getExercise1116B() throws Exercise1116B
{throw new Exercise1116B("Exercise1116B");
}
public static void getExercise1116C() throws Exercise1116C
{throw new Exercise1116C("Exercise111C");
}
}

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

@SuppressWarnings("serial")
/**
 *
 * @author Lauren
 */
class Exercise1116A extends Exception {
        public Exercise1116A(String message) {
                super(message);
        }
}

@SuppressWarnings("serial")
/**
 *
 * @author Lauren
 */
class Exercise1116B extends Exercise1116A {
        public Exercise1116B(String message) {
                super(message);
        }
}

@SuppressWarnings("serial")
/**
 *
 * @author Lauren
 */
class Exercise1116C extends Exercise1116B {
        public Exercise1116C(String message) {
                super(message);
        }
}

public class Exercise1116 {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
                // TODO code application logic here
                try {
                        getExercise1116B();
                } catch (Exercise1116A ea) {
                        System.out.println("Caught exception A.");
                        ea.printStackTrace();
                }
                
                try {
                        getExercise1116C();
                } catch (Exercise1116A ea) {
                        System.out.println("Caught exception A.");
                        ea.printStackTrace();
                }
                
                System.out.println("Ending program normally");
        }

        public static void getExercise1116B() throws Exercise1116B {
                throw new Exercise1116B("Exercise1116B");
        }

        public static void getExercise1116C() throws Exercise1116C {
                throw new Exercise1116C("Exercise111C");
        }
}

Your exception A, B, C class should be extending the java.util.Exception class. Then only they can be thrown from your code and be caught.. You can check the screenshot for run.. it works fine.. i have highlighted the code change.

Add a comment
Know the answer?
Add Answer to:
What is wrong with this code? Please bold the changes that were made so this code...
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
  • 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...

  • Need some help on the following problem in java. I have to modify the code I...

    Need some help on the following problem in java. I have to modify the code I written so far(below) to do the following three things: a). Write a method called diagnostics that occasionally throws any one of the exceptions you have created depending on the values generated by a random-number generator. b). Write a method called display that calls diagnostics and provides exception handlers to display a message when an exception occurs. Otherwise, if an exception does not occur, method...

  • How would I alter this code to have the output to show the exceptions for not...

    How would I alter this code to have the output to show the exceptions for not just the negative starting balance and negative interest rate but a negative deposit as well? Here is the class code for BankAccount: /** * This class simulates a bank account. */ public class BankAccount { private double balance; // Account balance private double interestRate; // Interest rate private double interest; // Interest earned /** * The constructor initializes the balance * and interestRate fields...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...

  • Need help with writing a Client test in JAVA

    public class TutorialSpace {                  // slots — the number of available slots in a tutorial class         private int slots;         // activated — true if the tutorial class has been activated         private boolean activated;                  /**         * TutorialSpace(n) — a constructor for a tutorial class with n slots.         *          * @param n         */         public TutorialSpace(int n) {                 this.slots = n;                 this.activated = false;         }                  /**         * activate() — activates the tutorial class. Throws an exception if the         * tutorial class has already been activated.         *          * @throws NotActivatedException         */                  public void activate() throws NotActivatedException {                 if (activated) {                         throw new NotActivatedException("Already Activated");                 } else {                         activated = true;                 }         }                  /**         * reserveSlot()—enrol a student into the tutorial class by decreasing the         * number of slots left for enrolling in the class. Throws an exception if slot         * is either completely used or the tutorial class is not active.         *          * @throws EmptyException         */         public void reserveSlot() throws EmptyException {                                  if (!activated || slots == 0) {                         throw new EmptyException("Tutorial space is empty");                 } else {                         slots--;                 }         }                  /**         * slotsRemaining()—returns the number of slots remaining in the tutorial class.         */                  public int slotsRemaining() {                 return slots;         }          } public class NotActivatedException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;                  public NotActivatedException(String msg) {                 super(msg);         }          } public class EmptyException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;         public EmptyException(String msg) {                 super(msg);         }                  ...

  • In java write a simple 1-room chat server that is compatible with the given client code.

    In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...

  • Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list...

    Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list using bubble sort Do not modify the test code in each function. You can look at that code for some ideas for implementing the methods. import java.lang.Comparable; import java.util.*; public class IteratorExercise {       public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {        // first line to start you off        ListIterator<E> iit = c.listIterator(), jit;...

  • Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters...

    Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters in it (see ExtraneousStringException.java. This java file does not need to be modified). Copy paste the contents of ExtraneousStringException.java into a new .java file, uncomment the commented out code, and write your code inside the main method. In the driver class, keep reading strings from the user until the user enters “DONE.” You may read your input from a file, or use the console....

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