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
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 equals to or
greater than 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
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 classes make the
custom exception. But you cannot add any more methods in Main class.
class Main
public static void mainString 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.ioIOException
Input format
First line takes the integer input
Output format
Case : Division by negative number
Caught exception: java.ioIOException: Chained Exception: Wrong Input
taken
Original cause: DivisionException: Custom Exception: Division by
negative number
Case : Division by zero
Caught exception: java.ioIOException: Chained Exception: Wrong Input
taken
Original cause: DivisionException: Custom Exception: Division by zero
Case : Division by number greater than
Caught exception: java.ioIOException: Chained Exception: Wrong Input
taken
Original cause: DivisionException: Custom Exception: Division by
number greater than
Case : Division by number between and inclusive
Sample Test Cases
Input:
Output:
Input:
Output:
Caught exception: java.ioIOException: 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
