3. Answer the following questions.
(a) What do you know about Exception Handling?
(b) Enumerate reasons why Exception Handling mechanism is essential
in program development
(c) Write a java application that computes the roots of a first
order polynomial equation. Note that your program should provide an
exception handling mechanism for the program especially when a
user-supplied value for variable “a” in the equation is zero.
Hint: ?=−?±√?2−4??2?
Answer a)
In computing and computer programming, exception handling is the process of responding to the occurrence of exceptions – anomalous or exceptional conditions requiring special processing - during the execution of a program. In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler; the details of how this is done depend on whether it is a hardware or software exception and how the software exception is implemented. It is provided by specialized programming language constructs, hardware mechanisms like interrupts, or operating system (OS) inter-process communication (IPC) facilities like signals. Some exceptions, especially hardware ones, may be handled so gracefully that execution can resume where it was interrupted.
Answer b)
Exception handling is important because it helps maintain the normal, desired flow of the program even when unexpected events occur. If exceptions are not handled, programs may crash or requests may fail. This can very frustrating for customers and if it happens repeatedly, you could lose those customers.
So if, for example, a program is written to calculate the average of some set of numbers - by adding them all together, then dividing by the number of numbers. But suppose the user doesn’t enter any numbers - if the programmer didn’t think to check how many numbers had been entered - then the computer might divide by zero.
Since division by zero is not a valid mathematical operation - the arithmetic unit will throw an exception rather than putting garbage in the result.
If it’s not handled, then the program will crash rather than continuing with garbage numbers being passed around.The benefit of this is that it ensures that the error happens right on the line of code where the mistake was made.
Answer c:
import java.util.Scanner;
public class Main {
public static void main(String[] Strings) {
double result=0;
Scanner input = new Scanner(System.in);
System.out.print("Input b: ");
double b = input.nextDouble();
System.out.print("Input c: ");
double c = input.nextDouble();
System.out.print("Input a: ");
double a = input.nextDouble();
if(a==0){
throw new ArithmeticException();
}else{
result = b * b - 4.0 * a * c;
if (result > 0.0) {
double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
} else if (result == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
} else {
System.out.println("The equation has no real roots.");
}
}
}
}
Get Answers For Free
Most questions answered within 1 hours.