Question

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 questionGet Homework Help C PART 1: (7 Marks) Cre PDAssignment 2 Q BOStart Assignment New tab X file:///C:/Users/ksimr/AppData/Local/output in red color and in the format of exception messages. This is a more common way to output error messages PART II: (8 M

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

Hello,

Please find the below code. Go through the inline code comments.

ExceptionHierarchy.java

===================

public class ExceptionHierarchy {
   public static void main(String[] args) {
       try
       {
           // Throwing ExceptionB and catching with ExceptionA
           throw new ExceptionB("ExceptionB occurs");      
       }
       catch (ExceptionA e) {
           System.err.println(e.getMessage());
       }
      
       try
       {
           //Throwing Exception C and catching with ExceptionA
           throw new ExceptionC("ExceptionC occurs");
           //Throwing Exception C      
       }
       catch (ExceptionA e) {
           System.err.println(e.getMessage());
       }
      
   }

}


ExceptionA.java

==============


public class ExceptionA extends Exception {
   public ExceptionA(String s) {
       super(s);
   }
}

ExceptionB.java

=============

/*
* ExcceptionB inherits from ExceptionA
*/
public class ExceptionB extends ExceptionA {

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

}

ExceptionC.java

==============

/*
* ExceptionC inherits from ExceptionB
*/
class ExceptionC extends ExceptionB {

   public ExceptionC(String s) {
       super(s);
   }

}

CatchDifferentExceptions.java

========================


public class CatchDifferentExceptions {
   public static void someMethod() throws ExceptionA {
       // Here i am creating random number in between 1 to 3
       int x = (int) (Math.random() * ((3 - 1) + 1)) + 1;
       if (x == 1) {
           System.out.println("Random Number is : "+x);
           throw new ExceptionA("ExceptionA Caught");
       } else if (x == 2) {
           System.out.println("Random Number is : "+x);
           throw new ExceptionB("ExceptionB Caught");
       } else {
           System.out.println("Random Number is : "+x);
           throw new ExceptionC("ExceptionC Caught");
       }

   }

   public static void main(String[] args) {
       for (int i = 1; i <= 3; i++) {
           try {
               // Call someMethod which create 3 different exception using random number generator
               someMethod();
           } catch (ExceptionC c) {
               System.err.println(c.getMessage());

           } catch (ExceptionB b) {
               System.err.println(b.getMessage());

           } catch (ExceptionA a) {
               System.err.println(a.getMessage());

           }

       }
   }
}

ExceptionA.java CatchDifferentExcepti - a (x)= Variables ® Breakpoints Go Expressions X ExceptionHierarchy.jav x ExceptionB.j

Exceptionc.java ExceptionA.java D CatchDifferentExcepti - ExceptionHierarchy.jav ExceptionB.java 83 10/* 2 * Excceptions inhe

3 Exception.java X ExceptionA.java CatchDifferentExcepti - 8 (x)= h D ExceptionHierarchy.jav ExceptionB.java 10/* 2 * ExceptiExceptionHierarchy.jav D ExceptionB.java D ExceptionC.java ExceptionA.java X CatchDifferentExcepti 2 public class ExceptionAExceptionHierarchy.java ExceptionB.java ExceptionC.java ExceptionA.java D CatchDifferentExceptions.java X 2 public class CatcResult of ExceptionHierarchy.java:

ExceptionA.java CatchDifferentExcepti ExceptionHierarchy.jav x ExceptionB.java D ExceptionC.java 1 public class ExceptionHier

Result of CatchDifferentExceptions.java

ExceptionHierarchy.jav ExceptionB.java D ExceptionC.java ExceptionA.java D CatchDifferentExcepti x = a (x)= Varia Name + Ad 2

Please let me know if you have any queries in the comments section.

Thank you.

Add a comment
Know the answer?
Add Answer to:
There is a white space in between but nothing is missing it's just the way screenshot...
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
  • 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...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

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

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • Program 2 #include #include using namespace std; int main() { int total = 0; int num...

    Program 2 #include #include using namespace std; int main() { int total = 0; int num = 0; ifstream inputFile; inputFile.open("inFile.txt"); while(!inputFile.eof()) { // until we have reached the end of the file inputFile >> num; total += num; } inputFile.close(); cout << "The total value is " << total << "." << endl; Lab Questions: We should start by activating IO-exceptions. Do so using the same method in Step 3 of the Program 1 assignment above, except that instead...

  • absolute C++ QUESTION 1 Which statement is incorrect? When an event occurs that cannot be managed...

    absolute C++ QUESTION 1 Which statement is incorrect? When an event occurs that cannot be managed locally, an exception may be thrown. Throwing the exception object transfers control and information gleaned locally to some calling program unit that manages the event, or the program terminates If an exception is thrown in a function, sayf, but not handled there, the exception is propagated to the function that called fo In C++, an exception object can be a user-defined type or any...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Part 2: Programming with Exceptions In this part of the lab , you will write a...

    Part 2: Programming with Exceptions In this part of the lab , you will write a program that fetches the information stored at a give URL on the web and saves that data to a file. This will also include networking and file operations and partly an exercise in using exceptions. For doing I/O, Java has a pair of nice abstractions: InputStream and OutputStream. These are abstract classes in the package java.io. An InputStream is a place from which you...

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