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
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());
}
}
}
}
Result of ExceptionHierarchy.java:
Result of CatchDifferentExceptions.java
Please let me know if you have any queries in the comments section.
Thank you.
There is a white space in between but nothing is missing it's just the way screenshot...
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) { 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 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 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: 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 = 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 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 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. ** 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 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...