Question: Write a C + + program that will implement the following tasks to help students understand how to use the try / catch block for
Write a C program that will implement the following tasks to help students understand how to use the trycatch block for exception handling. Make sure to use appropriate variable types and display the results clearly.
Division Function:
a Implement a function named "divideNumbers" that takes two integers as parameters: dividend and divisor.
b Inside the function, divide the dividend by the divisor and return the result.
c Handle the exception when the divisor is zero by throwing an exception of type "int" with a value of
User Input:
a Inside the main function, prompt the user to enter two integers: dividend and divisor.
b Read and store the input in suitable variables.
Exception Handling:
a Use the trycatch block to handle exceptions that may occur when calling the "divideNumbers" function.
b Inside the try block, call the "divideNumbers" function with the userprovided dividend and divisor.
c If an exception occurs, catch it in the catch block and display an appropriate error message indicating that division by zero is not allowed.
d If no exception occurs, display the result of the division.
Additional Functionality:
a Modify the "divideNumbers" function to throw an exception of type "string" with a descriptive error message if the dividend is negative.
b Add an additional catch block in the trycatch block to handle this new exception type.
c Display an appropriate error message indicating that a negative dividend is not allowed if this exception occurs.
Note: You are encouraged to use appropriate variable names, comments, and proper indentation in your code for better readability.
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
