Question: Problem 2 Difficulty: MEDIUM Problem Statement: Develop a Java program that demonstrates the concept of exception chaining and custom exceptions. The program should include a

Problem 2
Difficulty: MEDIUM
Problem Statement:
Develop a Java program that demonstrates the concept of exception chaining and
custom exceptions. The program should include a method named divideByNum() that
prompts the user to input a number. If the number is less than 0, equals to 0, or
greater than 100, a custom exception DivisionException (extended from
ArithmeticException) should be thrown with a specific message for each case. If the
number does not meet these conditions, perform a multiplication operation with 100
and the input number and print the result. If a DivisionException occurs during this
operation, catch it and use it to initialize an IOException with a specific message. This
new exception should then be chained with the original exception using the initCause
method. The chained exception should then be thrown and caught in the main method,
where the message of the new exception and the original cause should be printed to
the console.
Code Structure
Use this syntax for writing your solution. You may define other class(es) make the
custom exception. But you cannot add any more methods in Main class.
class Main {
public static void main(String[] args){
// main logic as described
}
public static void divideByNum() throws Exception {
// divideByNum logic as described
}
}
NOTE
You may only make use of the following packages wherever necessary, no other
packages are allowed:
java.util.Scanner
java.io.IOException
Input format
First line takes the integer input
8
Output format
Case 1: Division by negative number
Caught exception: java.io.IOException: Chained Exception: Wrong Input
taken
Original cause: DivisionException: Custom Exception: Division by
negative number
Case 2: Division by zero
Caught exception: java.io.IOException: Chained Exception: Wrong Input
taken
Original cause: DivisionException: Custom Exception: Division by zero
Case 3: Division by number greater than 100
Caught exception: java.io.IOException: Chained Exception: Wrong Input
taken
Original cause: DivisionException: Custom Exception: Division by
number greater than 100
Case 4: Division by number between 1 and 100(inclusive)
12300
Sample Test Cases
Input:
8
Output:
800
Input:
-2
Output:
Caught exception: java.io.IOException: Chained Exception: Wrong Input
taken
Original cause: DivisionException: Custom Exception: Division by
negative number
please solve this and please give full code and when i run it should show the correct output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!